0

I have a form that you can add fields: http://jsfiddle.net/ytrkqr6a/2/

 $(document).ready(function() {
 var max_fields = 32; //maximum input boxes allowed
 var wrapper = $(".cameras"); //Fields wrapper
 var add_button = $(".add-camera"); //Add button ID
 var x = 1; //initlal text box count
 $(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="form-group"><input type="text" name="camera '+ x +'" value="Camera '+ x +'" placeholder="Camera '+ x +'" class="form-control cameras" readonly /> <a href="#" class="remove-camera">Remove</a></div>'); //add input box
 }
 });
 $(wrapper).on("click",".remove-camera", function(e){ //user click on remove text
 e.preventDefault(); $(this).parent('div').remove(); x--;
 })
});
<div class="cameras">
 <div class="camera-field"><button class="add-camera btn btn-primary">+ Add Camera</button></div>
 <div style="clear:both;"></div>
 <div class="form-group"><input type="text" name="camera" value="" placeholder="Camera 1" class="form-control cameras" readonly /></div>
 </div>

Everything works fine adding fields, but where I'm hung up is when you remove one of the fields then numerical order gets off.. how do I make it update the remaining fields to stay in proper numerical order each time you add or remove a field:

Camera 1
Camera 2
Camera 3
etc...

Thanks ahead of time!

DJ Burb
2,3882 gold badges30 silver badges40 bronze badges
asked Dec 1, 2014 at 22:12

1 Answer 1

1

I just added a jQuery "each" loop to re placerholder, name, and value each input field every time a remove link is clicked. When using each the value passed to the function is the index of item in the loop's current iteration, I used the variable elm. Since each input field is classed with camerasCounter, you can use $(this) to easily call the input element in the loop. Each is a very useful part of jQuery, if you ask me.

http://jsfiddle.net/v76zn30o/2/

The HTML

<div class="cameras">
 <div class="camera-field"><button class="add-camera btn btn-primary">+ Add Camera</button></div>
 <div style="clear:both;"></div>
 <div class="form-group"><input type="text" name="camera" value="" placeholder="Camera 1" class="form-control camerasCounter" readonly /></div>
 </div>

and The jQuery

$(document).ready(function() {
var max_fields = 32; //maximum input boxes allowed
var wrapper = $(".cameras"); //Fields wrapper
var add_button = $(".add-camera"); //Add button ID
var x = 1; //initlal text box count
 $(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="form-group"><input type="text" name="camera '+ x +'" value="Camera '+ x +'" placeholder="Camera '+ x +'" class="form-control camerasCounter" readonly /> <a href="#" class="remove-camera">Remove</a></div>'); //add input box
 }
 });
 $(wrapper).on("click",".remove-camera", function(e){ //user click on remove text
 e.preventDefault(); $(this).parent('div').remove();
 $(".camerasCounter").each(function(elm){
 x = elm + 1
 $(this).attr({
 "placeholder": "Camera " + x,
 "name": "Camera " + x, 
 "value": "Camera " + x
 }); 
 });
 });
});
answered Dec 1, 2014 at 22:46
Sign up to request clarification or add additional context in comments.

2 Comments

If I wanted to add "options" to each camera, how would I make it so that each Name value = the name value of the camera? So that when I go to process the form, I can pull checkmarked options for each camera that is added: jsfiddle.net/Lxqvmg4w
I updated your jsfiddle: jsfiddle.net/Lxqvmg4w/2 You add the counter to each input item in your form-group class. You loop through the form-group class, then add the counter to each input element in the form-group.

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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.