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
Use .prev()
to go back one element.
alert($(".table tr:last").prev().find("td").html());
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());
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With