Skip to content

Instantly share code, notes, and snippets.

@haggen
Created June 1, 2014 02:58
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 haggen/c82f6a8de00d5531caef to your computer and use it in GitHub Desktop.
Save haggen/c82f6a8de00d5531caef to your computer and use it in GitHub Desktop.
(function($, undefined) {
'use strict';
function Repeating(container) {
this.container = $(container);
this.template = this.items().first().clone();
this.template.find('input, select, textarea').val('');
}
Repeating.prototype = {
items: function() {
return this.container.find('[data-repeating-item]');
},
recount: function() {
this.items().each(function(index) {
$('input, select, textarea').attr({
id: function(i, value) {
return value.replace(/_\d+_/, '_' + index + '_');
},
name: function(i, value) {
return value.replace(/\[\d+\]/, '[' + index + ']');
}
});
});
},
remove: function(child) {
$(child).parents('[data-repeating-item]').remove();
},
add: function() {
this.container.append(this.template.clone());
},
};
$.fn.repeating = function() {
return this.each(function() {
new Repeating(this);
});
};
$('[data-repeating-container]').repeating();
})(this.jQuery);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment