jQuery API

.last()

.last() Returns: jQuery

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

  • version added: 1.4.last()

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

Consider a page with a simple list on it:

<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 set of list items:

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

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

Example:

Highlight the last span in a paragraph.

<!DOCTYPE html>
<html>
<head>
  <style>.highlight{background-color: yellow}</style>
  <script src="http://code.jquery.com/jquery-latest.js"></script>
</head>
<body>
  <p><span>Look:</span> <span>This is some text in a paragraph.</span> <span>This is a note about it.</span></p>
<script>$("p span").last().addClass('highlight');</script>

</body>
</html>

Demo:

Support and Contributions

Need help with .last() or have a question about it? Visit the jQuery Forum or the #jquery channel on irc.freenode.net.

Think you've discovered a jQuery bug related to .last()? Report it to the jQuery core team.

Found a problem with this documentation? Report it on the GitHub issue tracker

  • Anonymous

    This is amazing! Exactly what I need to quickly and lazily get the borders off the last item in my lists

    • naugtur

      You always could – with pure CSS selector last

      • http://www.learningjquery.com/ Karl Swedberg

        Well, I suppose you could use the :last-child selector, but that isn’t really the same as .last() and it isn’t supported by IE 6, 7, or 8.

        • http://leaverou.me Lea Verou

          :first-child is supported by IE7+ and in most cases you can easily convert your CSS to use that instead.

          Also, you can use li + li to target all list items EXCEPT first child, and that works fine in IE7+ too.

          • http://www.learningjquery.com/ Karl Swedberg

            ok, but neither of those examples has anything to do with the .last() method.

  • AreN

    Before 1.4 one would have to do the following:
    var elements = $(“div”);
    var lastElement = elements.eq(elements.length);

    Note that using $(“ul li”).last() is not the same as $(“ul li:last-child”) (It had me somewhat confused)

    • http://www.learningjquery.com/ Karl Swedberg

      Minor correction to your example:
      var elements = $(“div”);
      var lastElement = elements.eq(elements.length - 1);

  • Sergey

    Isn’t it just

    var lastElement = $(“div:last”);

    or

    var elements = $(“div”);
    var lastElement = elements.filter(“:last”)

    ?

    • http://profiles.yahoo.com/u/POC5XYGMH3RARXD4AM6SKP7JE4 Kaleb

      Still trying to figure this one out.
      Is there any difference between ex:
      $(“a.link:last”).text();
      and
      $(“a.link”).last().text();

      • Collar

        There is no difference in your example, however if you have:
        $(“a.link:last, a.otherLink:last”);
        and
        $(“a.link, a.otherLink”).last();

        Then the first would return up to two object, where as the second would only return up to one, being the last link which had either of those classes.

  • Collar

    There is no difference in your example, however if you have:
    $(“a.link:last, a.otherLink:last”);
    and
    $(“a.link, a.otherLink”).last();

    Then the first would return up to two object, where as the second would only return up to one, being the last link which had either of those classes.