jQuery Traversing - last() Method
Example
Highlight the last span in the paragraph:
$("p span").last().addClass('highlight');
Definition and Usage
last() reduces the matched element collection to the last element in the set.
Syntax
.last()
Detailed Description
If a jQuery object representing a collection of DOM elements is given, the .last() method will use the last matching element to construct a new jQuery object.
Consider the following page with a simple list:
<ul> <li>list item 1</li> <li>list item 2</li> <li>list item 3</li> <li>list item 4</li> <li>list item 5</li> </ul>
We can apply this method to the list item collection:
$('li').last().css('background-color', 'red');
The result of this call is that the last item is set to a red background.