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
Fundamentals
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.
Concept
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.
Foundation
📝 Syntax
Official jQuery API form (since 1.0; deprecated 3.4):
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
Hands-On
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.
First Row, Middle Row → normal styling
Last Row → yellow background, bold text
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.
Settings → bold blue (last li in ul.nav)
Home in second nav list → unchanged
Other items → default styling
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.
Applications
🚀 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.
Important
📝 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.
Compatibility
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 ChromeAll versions · Desktop & Mobile
Full support
Mozilla FirefoxAll versions · Desktop & Mobile
Full support
Apple SafariAll versions · macOS & iOS
Full support
Microsoft EdgeAll versions · Chromium & Legacy
Full support
Internet ExplorerIE 6+ · Legacy environments
Full support
OperaAll modern versions
Full support
:lastDeprecated
Bottom line: Migrate to .last() — select with CSS, then filter in jQuery.
Wrap Up
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.
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
Summary
Key Takeaways
Knowledge Unlocked
Five things to remember about :last
Last element in the matched set only.
5
Core concepts
101
:last
One match
API
-102
= :eq(-1)
Equivalent
Rule
≠03
Not :last-child
Per parent differs
Compare
demo04
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.