add and remove form elements
Hi thanks for taking to the time to help me on this. I have some jquery code (see below)
- 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
- $(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="col-xs-12"><div class="row"><div class="col-xs-9"><div class="form-group"><label>Postage Description:</label><input type="text" class="form-control" value=""/></div><!-- form-group --></div><!-- col-xs-9 --><div class="col-xs-2"><div class="form-group"><label>Postage Price:</label><input type="text" class="form-control" value=""/></div><!-- form-group --></div><!-- col-xs-2 --><div class="remove_field"><span class="glyphicon glyphicon-remove"></span><div></div><!-- row --></div><!-- col-xs-12 -->'); //add input box
- }
- });
- $(wrapper).on("click",".remove_field", function(e){ //user click on remove text
- e.preventDefault(); $(this).parent('div').remove(); x--;
- })
the code adds html which contains form elelments (input) is there anyway to add name="" and id="" to each form field? preferably with a prefx? something like;
var prefix = anything
so when the plus sign is clicked to add the elements to the dom the input fields would have name="anything_01" id="anything_01" then the next one which is added has name="anything_02" id="anything_02" etc... etc...
help much appreciated.
Many thanks