i need to select the value of second to last input selectable element:
<tr><td><select class="x">...</select></td></tr>
<tr><td><select class="x">...</select></td></tr>
<tr><td><select class="x">...</select></td></tr>
The select input tag are inside tr tags.
If I use $("select.x").last()
, jQuery select the last element. I need to select second to last.
You can use
.eq()
with negative indexes:These negative indexes are "1-indexed":
-1
gives the last element,-2
the penultimate, and so on.All of the below will do the trick (select the second last element):
You need to use "nth-last-child(2)" of jquery, this selects the second last element.
You can check this here:
https://api.jquery.com/nth-last-child-selector/
The solutions with .prev() or nth-last-child() don't works.
The problem is the last().prev() functions return the the object
<a>
which i suppouse come first the select one.The nth-last-of-type(2) selector instead return an empty object.
You can use .prev()
You can use
:last
selector and move to the preceding element usingprev
:Ref:
Sample demo: http://jsfiddle.net/IrvinDominin/ck8XP/