Skip to content

Instantly share code, notes, and snippets.

@havvg
Last active December 21, 2017 16:55
Show Gist options
  • Star 5 You must be signed in to star a gist
  • Fork 3 You must be signed in to fork a gist
  • Save havvg/31ab826a5dc68877462c to your computer and use it in GitHub Desktop.
Save havvg/31ab826a5dc68877462c to your computer and use it in GitHub Desktop.
jQuery(function($) {
$(document).on('click', '.btn-add[data-target]', function(event) {
var collectionHolder = $('#' + $(this).attr('data-target'));
if (!collectionHolder.attr('data-counter')) {
collectionHolder.attr('data-counter', collectionHolder.children().length);
}
var prototype = collectionHolder.attr('data-prototype');
var form = prototype.replace(/__name__/g, collectionHolder.attr('data-counter'));
collectionHolder.attr('data-counter', Number(collectionHolder.attr('data-counter')) + 1);
collectionHolder.append(form);
event && event.preventDefault();
});
$(document).on('click', '.btn-remove[data-related]', function(event) {
var name = $(this).attr('data-related');
$('*[data-content="'+name+'"]').remove();
event && event.preventDefault();
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment