Skip to content

Instantly share code, notes, and snippets.

@Hampei
Created June 16, 2012 21:27
Show Gist options
  • Save Hampei/2942537 to your computer and use it in GitHub Desktop.
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.
# 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