Skip to content

Instantly share code, notes, and snippets.

@damien
Created September 21, 2010 20:58
Show Gist options
  • Select an option

  • Save damien/590532 to your computer and use it in GitHub Desktop.

Select an option

Save damien/590532 to your computer and use it in GitHub Desktop.
Form interaction plugin inspired by Vidget's 'Single-Use JQuery' Plugins post: http://www.viget.com/extend/single-use-jquery-plugins/
(function($) {
$.fn.cloneableFields = function() {
return this.each(function() {
var container = $(this);
var fields = container.find('li:last');
var label = 'Add another ' + fields.find('label').text();
container.count = function() {
return this.find('li').size();
};
// When link is clicked, add a new set of fields and set their keys to
// the total number of fieldsets, e.g. instruction_attributes[5][name]
var addLink = $("<a/>").text(label).click(function() {
html = fields.html().replace(/\[\d+\]/g, "[" + container.count() + "]");
$(this).before("<li>" + html + "</li>");
return false;
});
container.append(addLink);
});
};
})(jQuery);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment