If elem is the parent of your cloned structure, and cntr is the counter you said you maintain, you can correct all identifiers in this cloned structure as follows:
function fixIds(elem, cntr) { $(elem).find("[id]").add(elem).each(function() { this.id = this.id + cntr; }) }
If the identifiers may already have a cloned number at the end, and you want to replace that number, you can do it like this:
function fixIds(elem, cntr) { $(elem).find("[id]").add(elem).each(function() { this.id = this.id.replace(/\d+$/, "") + cntr; }) }
So, based on the code you added to your question, you can do this:
<script> var cloneCntr = 1; $("button").click(function () { var table = $("#table").clone(true,true) fixIds(table, cloneCntr); table.insertAfter("#table") cloneCntr++; }); </script>
Working example: http://jsfiddle.net/jfriend00/wFu9K/
Note. I also changed the DOM insertion code to insertAfter() , because you cannot add one table to another as you did (the table will either go behind the existing table or inside the cell in the previous table).
If you are just trying to add a row, you need to clone the row, not the whole table.
You can add a cloned row to an existing table with this code:
function fixIds(elem, cntr) { $(elem).find("[id]").add(elem).each(function() { this.id = this.id.replace(/\d+$/, "") + cntr; }) } var cloneCntr = 1; $("button").click(function () { var row = $("#table tr").first().clone(true,true); fixIds(row, cloneCntr++); row.appendTo("#table") });