Created
July 13, 2011 19:12
-
-
Save mhenke/1081078 to your computer and use it in GitHub Desktop.
jquery add new record
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
$(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"); | |
}); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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