jQuery .last() method returns the last element in the matched set

Syntax and Description

.last()

reduces the set of matched elements to the final one in the set. It return value is the new jQuery object.

Consider a page with a simple list as follows:


<ul>/*from  w ww.  j  a  v  a  2s. c  om*/
 <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>

When applying the following code

$('li').last().css('background-color', 'red');

It marks the final list item to red.





















Home »
  jQuery »
    jQuery Tutorial »




Basics
Selector
DOM
Event
Effect
Utilities