Skip to content

Instantly share code, notes, and snippets.

@aviflombaum
Created April 2, 2013 14:37
Show Gist options
  • Select an option

  • Save aviflombaum/5292703 to your computer and use it in GitHub Desktop.

Select an option

Save aviflombaum/5292703 to your computer and use it in GitHub Desktop.
$('form a.add_child').click(function() {
// The link that was clicked on has a data-association attribute that points it to the correct ID to clone.
var association = $(this).attr('data-association');
// Grab the template of the fields you want to copy, convert it to a string via the html() method
var template = $('#' + association + '_fields_template').html();
// Create a random number to replace the ID / name
var regexp = new RegExp('new_' + association, 'g');
var new_id = new Date().getTime();
// Replace and Insert
$(this).parent().before(template.replace(regexp, new_id));
return false;
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment