How to select last child element in jQuery?
Just the last child, not its descendants.
The last() function is an inbuilt function in jQuery which is used to find the last element of the specified elements. Here selector is the selected elements. Parameters: It does not accept any parameter. Return value: It returns the last element out of the selected elements.
You need to use "nth-last-child(2)" of jquery, this selects the second last element.
You can also do this:
<ul id="example"> <li>First</li> <li>Second</li> <li>Third</li> <li>Fourth</li> </ul> // possibility 1 $('#example li:last').val(); // possibility 2 $('#example').children().last() // possibility 3 $('#example li:last-child').val();
:last
.children().last()
:last-child
For perfomance:
$('#example').children().last()
or if you want a last children with a certain class as commented above.
$('#example').children('.test').last()
or a specific child element with a specific class
$('#example').children('li.test').last()
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