jQuery traversal - last() method
Example
Highlight the last span in the paragraph:
$("p span").last().addClass('highlight');
Definition and usage
last() reduces the matching element collection to the last element in the collection.
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.
Please think about 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 red background.