Skip to content

Instantly share code, notes, and snippets.

@Takaya213
Last active September 22, 2016 10:15
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Takaya213/9b96a0b6308e1e6663c980cc87a49e3c to your computer and use it in GitHub Desktop.
Save Takaya213/9b96a0b6308e1e6663c980cc87a49e3c to your computer and use it in GitHub Desktop.
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