Search code examples
jqueryloopsclone

JQuery Loop Clone


Trying to get my 1 table row to automatically clone exactly 24 times and then get the "add row +" button to clone additional.

Example can be found here: http://jsfiddle.net/CzZxf/17/

var uniqueIds = $("#math-table tr").length;
$("#button").click(function(){
var $thisRow = $(this).closest("tr"),
$clone = $thisRow.removeClass().clone(),             // Clone row
$inputs = $clone.find("input").val("").removeClass();
uniqueIds++; //Increment ID
$inputs[0].id = "A" + uniqueIds;
$inputs[1].id = "B" + uniqueIds;
$inputs[2].id = "C" + uniqueIds;


$thisRow.after($clone);                    
});

Solution

  • You are misunderstanding .closest, it works similarly to .parents.

    I fixed the traversing to find last tr here: http://jsfiddle.net/CzZxf/19/

    var $thisRow = $("#math-table tr:last-child")