Hey all. I am adding a "More Rows" button to a table within a long, complex form, and to add the rows, I am cloning an existing row in the table, modifying it, then appending it to the end of the table.
The row contains inputs that have id and name attributes that are numbered sequentially (e.g. id="Item_Name1" and name="Item_Name1" for the first row, id="Item_Name2" and name="Item_Name2" for the second row, etc.). I am looking for the best way of continuing this numbering sequence in the appended rows.
Here's the jQuery:
jQuery.noConflict();
jQuery(document).ready(function($){
var i = $("table#table-id tbody tr").length;
$("button#more-rows").click(function() {
var clonedRow = $("table#table-id tbody tr:first").clone();
i++;
$("*[name*='1']", clonedRow).attr('id', function() {
var attrval = $(this).attr("id");
var attrval = attrval.replace( /\d/g , "" );
return attrval + i
});
$("table#table-id").append(clonedRow);
});
});
This works for the id's, but how do I also increment the name attributes? Any tips on writing this more simply and/or more performantly are also appreciated.