Today I needed to write a function in jQuery to find and replace input value by name if it exists, otherwise append the input field to my html.
$('tr').click(function() {
var href = $(this).find("a").attr("href");
if(href) {
foundedit = $('input[name=permission_edit]').val();
if(foundedit){
$('input[name=permission_edit]').val(function(index, value) {
return value.replace(/\d+/,href);
});
} else {
$("#permissions_form").append("<input type=\"hidden\" id=\"permission_edit\" name=\"permission_edit\" value=" + href + " />");
}
}
});