5

I am using javascript to create input fields dynamically with a limit of 10 allowed on my form. What I need to do to ensure these fields are submitted to the correct place is give them the right ID. My question is how do I do this?

$(document).ready(function () {
    var max_fields = 10; //maximum input boxes allowed
    var wrapper = $(".input_fields_wrap"); //Fields wrapper
    var add_button = $(".add_field_button"); //Add button ID
    var x = 1; //initlal text box count

    var num = new Number;
    var newNum = num + 1;        

    /*if (x = max_fields) {
        alert("You can't add anymore fields.")   
    }
    */

    $(add_button).click(function (e) { //on add input button click
        e.preventDefault();
        if (x < max_fields) { //max input box allowed
            x++; //text box increment
            $(wrapper).append('<div class="clonedInput"><input id="" type="text" name="mytext[]"/><a href="#" class="remove_field">Remove</a></div>'); //add input box
        }
    });

    $(wrapper).on("click", ".remove_field", function (e) { //user click on remove text
        e.preventDefault(); $(this).parent('div').remove(); x--;
    })
});

It would be nice for each input box to have an ID like "data_item_1" , "data_item_2", "data_item_3" etc etc. I'm not sure on how to do this though.

2 Answers 2

5

You can use global variable like x to generate unique ids. You could have used x but as you are decrementing x so you may need to use separate variable.

$(wrapper).append('<div class="clonedInput"><input id="data_item_'+itemIndex+'"  type="text" name="mytext[]"/><a href="#" class="remove_field">Remove</a></div>'); //add input box

You code would be like

var itemIndex = 2;
$(add_button).click(function (e) { //on add input button click
    e.preventDefault();
    if (x < max_fields) { //max input box allowed
        x++; //text box increment

          $(wrapper).append('<div class="clonedInput"><input id="data_item_'+ itemIndex++ +'"  type="text" name="mytext[]"/><a href="#" class="remove_field">Remove</a></div>'); //add input box
    }
});
Sign up to request clarification or add additional context in comments.

7 Comments

What is itemIndex here?
Thanks for this, makes sense I think I was overengineering what I was doing. Do you know how I can set the value of the initial text box id as the ID's start at 2 onwards?
Initialize itemIndex = 2?
Lovely, all working now, many thanks. My initial approach was wrong and this has a much cleaner way. Over thinking my concept a bit.
Whilst this approach works I have noticed that removing a text input can throw the numbering out of sync and it's important that it remains between 1 and 10 only. Remvoing inputs and readding them can also cause ID duplication. Is there a way around this or should I perhaps raise a seperate question?
|
0

Try this line inside the click event

$(wrapper).append('<div class="clonedInput"><input id="data_item_'+x+'" type="text" name="mytext[]"/><a href="#" class="remove_field">Remove</a></div>'); 

1 Comment

Ids will be duplicate if user deletes and items let say if user added 3 items and deleted 2nd items and x-- set value to 3 and now when user add new items two fields have same id (data_item_3) as per your answer;

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.