Created
June 16, 2012 21:27
-
-
Save Hampei/2942537 to your computer and use it in GitHub Desktop.
jquery code to add repeating form parts. Usually for an array of items. Adds a link to add another row.
This file contains 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
# Use for serialized Array attributes, to add a link to add rows for it in the form. | |
# Last child is saved and appended on click. | |
# .field.repeat_last{data: {'link-name' => 'add item'}} | |
# .item | |
# = f.text_field :name | |
# = f.text_field :url | |
$(-> | |
$('form .repeat_last').map(-> | |
el = $(this) | |
last_child = el.find('>*:last-child').clone() | |
lnk = $('<a>').html(el.data('link-name') || 'add').click(-> | |
el.append(last_child.clone()) | |
) | |
el.after(lnk) | |
) | |
) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment