The jQuery last() method can be used to return the last item of the selected element. jQuery select last element from the selected group element whether it is table, list, or form.
jQuery last() Method
jQuery select the last element with the last()
method as given here below.
Syntax
The syntax of this method is given below:-
Description of the Parameters
The description of these parameters is given below.
Parameter Name | Description |
---|---|
selector | Specify the group element to select its last item. You can use the element tag name, class name, or id to select. It is a required field. |
jQuery last() Method to Select Last Element
If you want to select the last element of the selected group element, you have to use the method as given below. It doesn’t require any parameters to specify and use the method. After selecting the last element, you can apply CSS using the css() of jQuery.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
<script> $(document).ready(function(){ $('.mybtn').click(function(){ $("ul li").last().css("color","red"); }); }); </script> <ul> <li>First Item</li> <li>Second Item</li> <li>Third Item</li> <li>Last Item</li> </ul> <button type="button" class="mybtn">Click to Select Last</button> |
Output
- First Item
- Second Item
- Third Item
- Last Item
You have to click the button given above to selects the last item of the list element. The above example applies the red color to the selected last list item.
Related jQuery Methods
I hope you like this tutorial on jQuery last() method.
References