Getting the last row of a table using jQuery?
I'm dynamically creating table rows. So each time I add a row, I need to get the ID of the last <tr>
so that I can create a new <tr>
with ID = "(the last row's ID) + 1"
. Is there any way to get the last row's ID using jQuery?
$('#yourtableid tr:last').attr('id');
var id = $('table tr:last').attr('id');
Old question, but here's a different way using JQuery traversal which is a bit quicker:
$("#TableId").find("tr").last();
An alternative method to ID your rows, if they are all sequential is just to get the number of rows + 1.
$("#TableId").find("tr").length + 1
2019
As of jQuery 3.4.0 :last
is deprecated, use .last() instead, like this:
var id = $('#mytable tr').last().attr('id');
var tid=$('#tableid tr:last').attr('id');
alert(tid);