jQuery API

:last Selector

last selector

version added: 1.0jQuery(':last')

Description: Selects the last matched element.

Note that :last selects a single element by filtering the current jQuery collection and matching the last element within it.

Additional Notes:

  • Because :last is a jQuery extension and not part of the CSS specification, queries using :last cannot take advantage of the performance boost provided by the native DOM querySelectorAll() method. To achieve the best performance when using :last to select elements, first select the elements using a pure CSS selector, then use .filter(":last").

Example:

Finds the last table row.

<!DOCTYPE html>
<html>
<head>
  <script src="http://code.jquery.com/jquery-latest.js"></script>
</head>
<body>
  <table>

    <tr><td>First Row</td></tr>
    <tr><td>Middle Row</td></tr>
    <tr><td>Last Row</td></tr>

  </table>
<script>$("tr:last").css({backgroundColor: 'yellow', fontWeight: 'bolder'});</script>

</body>
</html>

Demo:

Support and Contributions

Need help with :last Selector 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 Selector? Report it to the jQuery core team.

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

  • http://profiles.yahoo.com/u/HMAL5BMKR2BMKZVQEQ24RR7DFE Space

    Is there a way of saying not:last as a selector?

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

      Yes. $('something:not(:last)') or $('something').not(':last') or $('something').slice(0, -1). I'm sure there are others.

      • ambi

        how to get last row of a table out of many tables? is there any way to specify table id? please reply fast :(

        • Vinita

          try
          $(“#tableid tr:last”)

  • http://profiles.yahoo.com/u/HMAL5BMKR2BMKZVQEQ24RR7DFE Space

    Is there a way of saying not:last as a selector?

  • Hsharif2

    Suppose there is a listbox and the user shift+clicks multiple selects, is there anyway to retrieve them all instead of only the very last one?

  • Ciantic

    Is this incorrectly named? Shouldn't it be “last-of-type” http://reference.sitepoint.com/css/pseudoclass-… though I like the brevity of “:last” but if being compatible with CSS is the first priority I think it's name should be last-of-type.

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

      This is a custom jQuery selector, not based on the CSS spec. The CSS :last-of-type selector is completely different.

      • Suit

        can you explain the difference? i don't get it

  • graceman9

    :last OR :last-child ?
    This confuse me.. Half an hour I spent searching for an answer:

    The :last selector selects the last element on the page matching the entire selector. The :last-child selector selects all last children matching the entire selector.

    http://www.sethcardoza.com/posts/view/43/title/jquery-selectors-last-and-last-child

  • graceman9

    :last OR :last-child ?
    This confuse me.. Half an hour I spent searching for an answer:

    The :last selector selects the last element on the page matching the entire selector. The :last-child selector selects all last children matching the entire selector.

    http://www.sethcardoza.com/pos…

  • Pasi Valkeila

    In IE7:
    var etusivuntuote=1;
    $('div.etusivu-nosto-left').each(function(){
    if (etusivuntuote == $('div.etusivu-nosto-left').length) {
    $(this).addClass('last');
    }
    etusivuntuote++;
    });