Find second last row from table
I have a table with a dynamic row and I want to find the second last row from the table.
HTML code
<table class="table">
<tr><td>1</td></tr>
<tr><td>2</td></tr>
<tr><td>3</td></tr>
<tr><td>4</td></tr>
<tr><td>5</td></tr>
<tr><td>6</td></tr>
<tr><td>7</td></tr>
<tr><td>8</td></tr>
<tr><td>9</td></tr>
<tr><td>10</td></tr>
</table>
JQuery code
alert($(".table").find("tr:last").find("td").html());
I tried with tr:last
, but it gave me the last line with the value 10
.
Expected Result
The second value of the last line 9
instead of10
JS Fiddle
Use .prev()
to go back to one item.
alert($(".table tr:last").prev().find("td").html());
var total = $(".table").find("tr").length;
alert($(".table").find("tr:nth-child("+(total-1)+")").find("td").html());
Fiddle work
With nth-last-child:
alert($(".table").find("tr:nth-last-child(2)").find("td").html());
You can also make it a single selector:
alert($(".table tr:nth-last-child(2) td").html());
try it
$('.table tr:last').prev('tr').html()
use .eq () and -1 indicates the last element ...
alert($(".table").find("tr").eq(-2).find("td").html());
Try it,
alert($(".table").find("tr:last").prev().find("td").html());
updated violin