Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Clone a section of form code and increment the name/is of the inputs.
<div class="input_fields_wrap">
<div>
<input type="text" name="fields[contact][new1][fields][contactName]" /><br />
<input type="text" name="fields[contact][new1][fields][contactSurname]" /><br />
<input type="text" name="fields[contact][new1][fields][contactEmail]" />
</div>
<button class="add_field_button">Add More Fields</button>
</div>
jQuery(document).ready(function($) {
var wrapper = $(".input_fields_wrap div"); //Fields wrapper
var add_button = $(".add_field_button"); //Add button Class/ID
var x = 1; //initlal text box count
$(add_button).click(function(e){ //on add input button click
e.preventDefault();
x++; //text box increment
var name = $(wrapper).children('input').attr('name').replace('[new1]', '[new'+x+']');
$(wrapper).last().clone()
.insertBefore(add_button).children('input').attr('name', name); //add input box
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment