Reduce the set of matched elements to the final one in the set.

.last()🡢 jQuery

Given a jQuery object that represents a set of DOM elements, the .last() method constructs a new jQuery object from the last element in that set.

Consider a page with a simple list on it:

  <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>

We can apply this method to the set of list items:

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

The result of this call is a red background for the final item.

Highlight the last item in a list.

  <li>This is some text in a list.</li>
  <li>This is a note about it.</li>
  <li>This is another note about it.</li>
.highlight {
  background-color: yellow;
$("ul li").last().addClass("highlight");

Looking for a Web Developer?


Hi! I'm Basti, author of this site. If you are looking for a web developer with 15+ years of experience, holla at me!

Be it the good 'ol jQuery, vanilla JS or modern frameworks like Vue and Svelte, front- or backend, I can help you.

Just write me at jobs@jqapi.com :)