Skip to content

Instantly share code, notes, and snippets.

@mhenke
Created July 13, 2011 19:12
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mhenke/1081078 to your computer and use it in GitHub Desktop.
Save mhenke/1081078 to your computer and use it in GitHub Desktop.
jquery add new record
$(function(){
var template = $('#riders-div .rider-new:first').clone(),
ridersCount = 0;
var addRider = function(){
ridersCount++;
var rider = template.clone().removeClass("dontshow").find(':input').each(function(){
var newId = this.id.replace('-', 's['+ ridersCount + '][') + ']';
$(this).prev().attr('for', newId); // update label for (assume prev sib is label)
this.name = this.id = newId; // update id and name (assume the same)
}).end() // back to .rider
.appendTo('#rider-exist'); // add to container
$('#rider-message').html('').removeClass("ui-state-highlight").removeClass("ui-state-error");
};
$('#addButton').click(addRider()); // attach event
$("#removeButton").click(function () {
$('#riders-div .rider-new:last').remove();
$('#rider-message').html('').removeClass("ui-state-highlight").removeClass("ui-state-error");
});
});
@mhenke
Copy link
Author

mhenke commented Jul 13, 2011

I would like to be able to pass in "rider" or something else and have add/remove do the same thing but for other items beside rider like subaccounts

@mhenke
Copy link
Author

mhenke commented Jul 13, 2011

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment