Skip to content

Instantly share code, notes, and snippets.

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">
<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]" />
<button class="add_field_button">Add More Fields</button>
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
x++; //text box increment
var name = $(wrapper).children('input').attr('name').replace('[new1]', '[new'+x+']');
.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