jQuery :last Selector

Beginner
⏱️ 9 min read
📚 Updated: Jul 2026
🎯 5 Examples
🚀 5 Try-it labs
jQuery extension · deprecated 3.4

What You’ll Learn

The :last selector matches exactly one element — the last in the matched set, in document order. Equivalent to :eq(-1). jQuery extension since 1.0; deprecated in 3.4 — prefer the .last() method.

01

Syntax

tr:last

02

One match

Single element

03

Official

tr yellow bold

04

= :eq(-1)

Equivalent

05

vs :last-child

Many per parent

06

.last()

Modern replacement

Introduction

When you have many matching elements — table rows, list items, or cards — you often need only the last one in document order. The jQuery :last pseudo-class narrows a matched set to that single trailing element.

jQuery has supported :last since version 1.0. Official documentation states it selects the last matched DOM element and is equivalent to :eq(-1). As of jQuery 3.4, the selector is deprecated — use .last() on a CSS-selected collection instead.

Understanding the :last Selector

Think of :last as “keep only the final index from this matched set”:

  • $("tr:last") → one row — the last tr in document order.
  • $("li:last") → one list item — the very last li on the page (in scope).
  • Equivalent to :eq(-1) per official jQuery docs.
  • Not the same as :last-child — that can match one element per parent.
💡
Beginner Tip

Need the last item in every list? Use $("ul li:last-child"), not $("li:last"). The former returns many; the latter returns one.

📝 Syntax

Official jQuery API form (since 1.0; deprecated 3.4):

jQuery
jQuery( ":last" )



// typical usage — prefix with element:

$( "tr:last" )

$( "ul.nav li:last" )

$( "div.card:last" )



// equivalents:

$( "tr:eq(-1)" )



// modern replacement (jQuery 3.4+ guidance):

$( "tr" ).last()

Parameters

  • No arguments — keeps the last element from the matched set that precedes :last.

Return value

  • A jQuery object containing zero or one element.
  • Empty collection when the preceding selector matched nothing.

Official jQuery API example

jQuery
$( "tr:last" ).css({

  backgroundColor: "yellow",

  fontWeight: "bolder"

});

⚡ Quick Reference

Goal:last.last()
Last table rowtr:last$("tr").last()
Same as :eq(-1)li:last$("li").eq(-1)
Last li in documentOne elementOne element
Last li in each listNot :lastUse li:last-child
Native CSSNot supportedjQuery method only

📋 :last vs :eq(-1), .last(), and :last-child

Set index vs sibling position — one global match vs one per parent.

:last
li:last

One element total

:eq(-1)
li:eq(-1)

Equivalent

.last()
$("li").last()

Modern replacement

:last-child
li:last-child

One per parent

Examples Gallery

Example 1 follows the official jQuery tr:last demo. Examples 2–5 cover equivalence with :eq(-1), migration to .last(), contrast with :last-child, and scoped list selection.

📚 Official jQuery Demo

Highlight the last table row in the matched set.

Example 1 — Official Demo: tr:last

Official jQuery demo — Last Row yellow and bold; First Row and Middle Row unchanged.

jQuery
$( "tr:last" ).css({

  backgroundColor: "yellow",

  fontWeight: "bolder"

});
Try It Yourself

How It Works

All tr elements form the matched set; :last keeps only the final index — the last row in document order. Official docs also show $("tr").last() as the modern equivalent.

Example 2 — Equivalence: :last and :eq(-1)

Official docs — both select the same single element from the matched set.

jQuery
console.log( ":last →", $( "li:last" ).text() );

console.log( ":eq(-1) →", $( "li:eq(-1)" ).text() );

console.log( "Same element?", $( "li:last" ).is( $( "li:eq(-1)" )[0] ) );
Try It Yourself

How It Works

:last and :eq(-1) both narrow to the element at the final zero-based index of the matched set.

📈 Practical Patterns

Modern migration, sibling contrast, and scoped queries.

Example 3 — Modern Code: .last() Method

jQuery 3.4+ guidance — replace :last with a CSS selector plus .last().

jQuery
// legacy (deprecated selector):

// $( "tr:last" ).css({ backgroundColor: "yellow", fontWeight: "bolder" });



// recommended:

$( "tr" ).last().css({

  backgroundColor: "yellow",

  fontWeight: "bolder"

});
Try It Yourself

How It Works

Separating the CSS selector from index filtering improves clarity and avoids deprecated pseudo-classes in selector strings.

Example 4 — :last vs :last-child

Show why :last returns one item globally while :last-child returns one per list.

jQuery
$( "li:last" ).addClass( "one" );

$( "li:last-child" ).addClass( "lc" );



console.log( ":last →", $( "li:last" ).length );

console.log( ":last-child →", $( "li:last-child" ).length );
Try It Yourself

How It Works

Official docs: while :last matches only a single element, :last-child can match more than one — one for each parent.

Example 5 — Scoped Query: ul.nav li:last

Limit scope so “last” means the last item in a specific navigation list.

jQuery
$( "ul.nav li:last" ).css({

  fontWeight: "bold",

  color: "#2563eb"

});
Try It Yourself

How It Works

Prefixing with ul.nav builds a smaller matched set — the last match is the trailing nav link, not the last li on the entire page.

🚀 Common Use Cases

  • Table footers — style tr:last or tfoot tr:last for emphasis.
  • Trailing list item — highlight the last entry in a flat collection with li:last.
  • Last card in a grid.card:last for featured layout (legacy code).
  • Legacy maintenance — understand :last when reading older jQuery projects.
  • Migration — replace with $("selector").last() in modern codebases.
  • Performance — select with CSS first, then .filter(":last") or .last().

🧠 How jQuery Evaluates :last

1

Build matched set

Evaluate the selector before :last — e.g. all tr elements.

Query
2

Order by document

Elements stay in document order — official docs note this explicitly.

Index
3

Keep final index

:last discards all but the last element — same as :eq(-1).

Filter
4

Return collection

Chain .css() or other jQuery methods on zero or one element.

📝 Notes

  • Available since jQuery 1.0 — deprecated in 3.4; prefer .last().
  • Equivalent to :eq(-1) — official jQuery documentation.
  • Matches only one element — unlike :last-child which can match many.
  • jQuery extension — not valid in native querySelectorAll.
  • For performance, use a pure CSS selector first, then .filter(":last") or .last().
  • Do not confuse with CSS :last-child or :last-of-type — different rules entirely.

Browser Support

The :last pseudo-class is a jQuery extension — not standard CSS — and works in jQuery 1.0+. It is deprecated in 3.4+; use .last() instead. The method works in all browsers that support jQuery.

jQuery 1.0+ · deprecated 3.4

jQuery :last Selector

Use $("tr").last() in new code — not tr:last in selector strings.

100% jQuery only
Google Chrome All versions · Desktop & Mobile
Full support
Mozilla Firefox All versions · Desktop & Mobile
Full support
Apple Safari All versions · macOS & iOS
Full support
Microsoft Edge All versions · Chromium & Legacy
Full support
Internet Explorer IE 6+ · Legacy environments
Full support
Opera All modern versions
Full support
:last Deprecated

Bottom line: Migrate to .last() — select with CSS, then filter in jQuery.

Conclusion

The :last selector matches exactly one element — the last in the matched set. The official tr:last demo highlights Last Row only with yellow background and bold text.

Remember equivalence with :eq(-1), distinguish it from :last-child, and migrate to .last() in jQuery 3.4+ projects.

💡 Best Practices

✅ Do

  • Use $("tr").last() in new jQuery code
  • Scope queries — ul.nav li:last when needed
  • Choose :last-child when every parent needs a last item
  • Remember :last equals :eq(-1) in legacy code
  • Select with CSS first for better performance

❌ Don’t

  • Use :last in new selector strings (deprecated 3.4)
  • Confuse :last with :last-child or :last-of-type
  • Assume :last returns one item per parent
  • Use bare $(":last") without a preceding selector
  • Expect :last to work in querySelectorAll

Key Takeaways

Knowledge Unlocked

Five things to remember about :last

Last element in the matched set only.

5
Core concepts
-1 02

= :eq(-1)

Equivalent

Rule
03

Not :last-child

Per parent differs

Compare
demo 04

Official

tr yellow bold

Demo
.last() 05

Deprecated

Use method

Tip

❓ Frequently Asked Questions

:last selects exactly one element — the last in the matched set that precedes it, in document order. $("tr:last") matches only the last table row among all tr elements in scope. Available since jQuery 1.0.
Yes. Official jQuery documentation states that :last is equivalent to :eq(-1). Both return a single element at the final index of the matched set.
Yes — deprecated in jQuery 3.4. Official docs recommend removing :last from selectors and using the .last() method after a pure CSS selector instead: $("tr").last() rather than $("tr:last").
:last returns one element total from the matched set. :last-child can return many — one per parent. Official docs highlight this distinction explicitly.
No. :last is a jQuery extension and not part of the CSS specification. It does not work in native querySelectorAll. Use .last() on a CSS-selected collection for modern code.
:last always follows a selector that builds the matched set — e.g. $("li:last") not $(":last"). Index selectors (:last, :eq, :lt, :gt, :even, :odd) narrow the set produced by what comes before them.
Did you know?

:last could also be written as :eq(-1) — negative indices count from the end of the matched set. Official jQuery docs list both forms as equivalent.

Continue to Child Selector

After index pseudo-classes, learn the > combinator for matching direct children only.

Child selector tutorial →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

6 people found this page helpful