2

Each time the button is clicked I need this to increment the name='0fName' by one so the php script sees a unique name and does not overwrite the previous item.

$(document).ready(function(){
    $('#addMember').click(function() {
        for (var i = 0; i <= 6; i++); 
        console.log("addMember was called");
        $("#addfamMember").append("<input type='text' name='0fName'>");
    return false;
});
1
  • Future note: that is invalid markup for for loop. Commented Apr 5, 2013 at 18:10

4 Answers 4

3

Add a counter:

$(document).ready(function(){
var count=0;
    $('#addMember').click(function() {
        console.log("addMember was called");
        $("#addfamMember").append("<input type='text' name='" + count + "fName'>");
        count++;
    return false;
});
Sign up to request clarification or add additional context in comments.

Comments

2

Another approach could be changing a name attribute to fName[]:

$('#addMember').click(function () {
    $("#addfamMember").append("<input type='text' name='fName[]'>");
    return false;
});

In this case data will be sent as fName array which may be more convenient to deal with than with a bunch of separate parameters.

Comments

1

Like epascarello says, add a counter:

$(document).ready(function(){

var counter = 0;

$('#addMember').click(function() {
    console.log("addMember was called");
    $("#addfamMember").append("<input type='text' name='" + counter + "fName'>");

    counter++;
    return false;
});

1 Comment

You need to fix name=counter + 'fName' then it'll be correct
1

.index() will do this too:

$('#addMember').click(function() {
    var idx = $(':input:last').index();
    $("#addfamMember").append("<input type='text' name='" + idx + "fName'>");
    return false;
});

Fiddle

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.