Skip to content

Instantly share code, notes, and snippets.

@agripinoduarte
Created February 5, 2013 20:06
Show Gist options
  • Save agripinoduarte/4717208 to your computer and use it in GitHub Desktop.
Save agripinoduarte/4717208 to your computer and use it in GitHub Desktop.
Incrementa campos hasmany-like usados nos formulários do cakephp
multiField = function (buttonId, fieldSelector) {
$('#' + buttonId).click(function() {
div = $(fieldSelector + ':last').clone();
inputs = $(div).find('input,select');
$(inputs).each(function(i,e) {
incrementInputIndexes(i,e)
});
$(fieldSelector + ':last').after(div);
});
}
incrementInputIndexes = function (i,e) {
index = $(e).attr('name').match(/[0-9]+/);
n = parseInt(index[0],10) + 1;
name = $(e).attr('name');
name = name.replace(/[0-9]+/, n + '');
$(e).attr('name', name);
$(e).val('');
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment