Created
September 21, 2010 20:58
-
-
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/
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| (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