Skip to content

Instantly share code, notes, and snippets.

@paperscissors
Created August 9, 2021 18:01
Show Gist options
  • Save paperscissors/0f6ea21437922379f8ddb56b5cdabfd4 to your computer and use it in GitHub Desktop.
Save paperscissors/0f6ea21437922379f8ddb56b5cdabfd4 to your computer and use it in GitHub Desktop.
jquery content repeater
$('.repeater').on('click', '.repeater-btn', function(e) {
e.preventDefault();
var $this = $(this),
$repeater = $this.closest('.repeater').find('[data-repeatable]'),
count = $repeater.length,
$clone = $repeater.first().clone();
$clone.find('[id]').each(function() {
this.id = this.id + '_' + count;
});
$clone.find('[name]').each(function() {
this.name = this.name + '[' + count + ']';
});
$clone.find('label').each(function() {
var $this = $(this);
$this.attr('for', $this.attr('for') + '_' + count);
});
$clone.insertBefore($this);
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment