JQuery clone second last row and insert at end of table

I am making code to dynamically add more rows to a table on click. I want to clone the last two rows of the table and then add them at the end. Of course, the table is dynamic, so there is no fixed number of rows. I have the last row clone, but I cannot get the second last row. How can I choose it?

$('.additional_row').live('click', function() { var $rows = $('#maintable tr'); var $secondLastRow = $rows[$rows.length - 2]; $('#maintable tbody>tr:nth-child(' + $secondLastRow + ')').clone(true).insertAfter('#maintable tbody>tr:last'); $('#maintable tbody>tr:last').clone(true).insertAfter('#maintable tbody>tr:last'); return false; }); 
+4
source share
1 answer

$('#maintable tbody>tr:last').prev('tr') will give you the second last

+10
source

All Articles