Skip to content

Instantly share code, notes, and snippets.

@fieldoffice
Last active August 23, 2016 11:17
Show Gist options
  • Save fieldoffice/96402bbc16384a7931dcd2fd749615de to your computer and use it in GitHub Desktop.
Save fieldoffice/96402bbc16384a7931dcd2fd749615de to your computer and use it in GitHub Desktop.
jQuery Clone
/* CLONE */
var regex = /^(.+?)(\d+)$/i;
var cloneIndex = $('.cloneme').length;
function clone(){
$(this).parents('.form-element-group').find('.cloneme').eq(0).clone()
.appendTo('.form-element-group-repeater')
.attr("id", "cloneme" + cloneIndex)
.find("*")
.each(function() {
var id = this.id || "";
var match = id.match(regex) || [];
if (match.length == 3) {
this.id = match[1] + (cloneIndex);
}
})
.find("input:text").val("").end()
.on('click', '.add-application', clone)
.on('click', '.remove-application', remove);
cloneIndex++;
}
function remove(){
$(this).parents('.cloneme').remove();
}
$('.add-application').on("click", clone);
$('.remove-application').on("click", remove);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment