Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Find second last row from table

I have table with dynamic row and I want to find second last row from 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 give me last row with value 10.

Expected Output

Second last row value 9 instead of 10

JS Fiddle

like image 705
Sadikhasan Avatar asked Dec 27 '14 10:12

Sadikhasan


2 Answers

Use .prev() to go back one element.

alert($(".table tr:last").prev().find("td").html());
like image 71
Barmar Avatar answered Sep 23 '22 06:09

Barmar


With nth-last-child:

alert($(".table").find("tr:nth-last-child(2)").find("td").html()); 

You could also make it a single selector:

alert($(".table tr:nth-last-child(2) td").html()); 
like image 22
zord Avatar answered Sep 24 '22 06:09

zord