Assuming that you will do it more often, and then once.
var counter = 0;
$("#add").click(function() {
counter++;
var cln = $('#ordertable tbody>tr:last').clone(true);
cln.find("[id^='prodcode'], [id^='meterage']").each(function(i, val) {
val.id = val.id.match(/^([^0-9]+)[0-9]*$/)[1] + "" + counter;
});
cln.insertAfter('#ordertable tbody>tr:last');
$('#ordertable tbody>tr:last #prodcode').val('');
$('#ordertable tbody>tr:last #meterage').val('');
$('td.imgsample:last a').remove();
return false;
});
Of course, there are “cleaner” solutions without using a global counter.
For the problem mentioned in the first comment, something along the lines
$("tr [id^='prodcode'], tr [id^='meterage']").live("blur", function() {
....
$(this).val()
....
});
should do
source
share