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

+3


source to share


6 answers


Use .prev()

to go back to one item.



alert($(".table tr:last").prev().find("td").html());

      

+6


source


var total = $(".table").find("tr").length;
alert($(".table").find("tr:nth-child("+(total-1)+")").find("td").html());

      



Fiddle work

+2


source


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()); 

      

+2


source


try it

$('.table tr:last').prev('tr').html()

      

+1


source


use .eq () and -1 indicates the last element ...

alert($(".table").find("tr").eq(-2).find("td").html());

      

+1


source


Try it,

alert($(".table").find("tr:last").prev().find("td").html());

updated violin

0


source







All Articles