The .first() traversing method reduces the current jQuery collection to the first element — index 0. This tutorial covers the official list highlight demos, compares .first() with .eq(0) and .last(), and shows how to chain after .filter() and .find().
01
Syntax
.first()
02
No args
Zero params
03
Index 0
First in set
04
vs .eq(0)
Same result
05
vs .last()
Opposite end
06
Since 1.4
Core API
Fundamentals
Introduction
After selecting multiple elements — list items, table rows, tabs, cards — you often need just the first one. Maybe you want to highlight the top menu item, focus the first input in a form, or style the leading row in a table. The .first() method picks that element in one readable step.
Available since jQuery 1.4, .first() takes no arguments. It constructs a new jQuery object from the element at index zero in the current set. It is shorthand for .eq(0) — same result, clearer intent when you always want the leading element.
Concept
Understanding the .first() Method
Given a jQuery object representing zero or more DOM elements, .first() returns a new jQuery object containing only the first element in that set. The index is always zero-based — the first matched element is at position 0.
If the collection is empty, .first() returns an empty jQuery object. If the collection has one element, that element is returned unchanged in a new wrapper. The method does not search the DOM again — it only looks at the current jQuery object.
💡
Beginner Tip
$("li").first() is equivalent to $("li").eq(0). Use .first() when readability matters; use .eq(0) when you are already working with index numbers in the same chain.
Foundation
📝 Syntax
General form of .first:
jQuery
.first()
Parameters
None — .first() does not accept any arguments.
Return value
A new jQuery object containing the first element in the current set, or an empty set if there are no matched elements.
Official jQuery API list example
jQuery
$( "li" ).first().css( "background-color", "red" );
// Red background on list item 1 only
Cheat Sheet
⚡ Quick Reference
Goal
Code
First element in set
$("li").first()
Same as eq(0)
$("li").eq(0)
Last element in set
$("li").last() or .eq(-1)
First after filtering
$("li").filter(".active").first()
First descendant match
$(".form").find("input").first()
Selector at query time
$("li:first")
Compare
📋 .first() vs .eq(0) vs .last() vs :first
Four ways to target the first element — readability, index, and timing.
.first()
index 0
Readable shorthand; no arguments
.eq(0)
explicit
Same result; pairs with .eq(-1)
.last()
final
Opposite end — last element only
:first
selector
Filter at initial query time
Hands-On
Examples Gallery
Examples 1–2 follow the official jQuery API documentation. Use DevTools or the Try-it links to run each snippet in the browser.
📚 Getting Started
Official jQuery demos for first-element selection.
Example 1 — Official Demo: Red Background on First List Item
Apply a red background to the first li in a simple list — the core official demo.
.first() and .eq(0) → both green on item 1
Same jQuery object length (1) after either call
How It Works
Internally, .first() is implemented as a call to .eq(0). Choose .first() for clarity; choose .eq(0) when mixing with other index values in the same chain.
Example 4 — .first() After .filter()
Get the first active list item from a filtered subset — a common real-world pattern.
First .active item → bold
Other .active items → normal weight
How It Works
.filter(".active") narrows to matching items. .first() then picks only the leading active item — useful for tab panels, breadcrumbs, or “primary” selection logic.
Example 5 — .first() After .find()
Focus the first input inside a form section after searching descendants.
First text input in .checkout → receives focus
Other inputs → unchanged
How It Works
.find() collects all matching inputs inside the container. .first() selects the top one in document order — a clean pattern for auto-focus on page load.
Applications
🚀 Common Use Cases
Highlight lead item — $("ul.menu li").first().addClass("active").
Auto-focus first field — $(".form").find("input").first().focus().
First visible tab — $(".tab").filter(":visible").first().
Read first data cell — $("tr").first().find("td").first().text().
🧠 How .first() Picks One Element
1
Start with collection
jQuery object holds zero or more elements.
Input
2
Read index 0
Take the first element in the set.
Index
3
Wrap or empty
One-element jQuery object, or empty if none.
Result
4
1️⃣
Return & chain
New jQuery object — continue with .css(), .addClass(), etc.
Important
📝 Notes
Available since jQuery 1.4 — no arguments accepted.
Equivalent to .eq(0) — shorthand for index zero only.
Operates on the current jQuery collection — does not re-query the DOM.
Empty input collection returns empty output — safe to chain.
Pushes onto jQuery’s internal stack — pair with .end() to restore the previous set.
For the last element, use .last() or .eq(-1) instead.
Compatibility
Browser Support
.first() has been part of jQuery since 1.4+. It relies on collection indexing with no browser-specific behavior.
✓ jQuery 1.4+
jQuery .first()
Supported in jQuery 1.x, 2.x, and 3.x across all modern browsers and IE with supported jQuery builds. Native equivalent: collection[0] wrapped back into jQuery.
100%With jQuery loaded
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
.first()Universal
Bottom line: Safe in any jQuery project. Use .first() for readability; use .eq(0) when index logic is already in play.
Wrap Up
Conclusion
The jQuery .first() method reduces a matched collection to the single element at index zero. It is the clearest way to target the leading item when you do not need a custom index.
Remember the official list lesson: only the first item gets the red background or highlight class. Pair .first() with .filter() or .find() to pick the leading match from a narrowed set, and use .last() for the opposite end.
Chain after .filter() to get the first matching item
Use .last() for the final element in the same style
Call .end() after .first() when continuing on the full set
Prefer .first() over .eq(0) for readability
❌ Don’t
Pass arguments to .first() — use .eq(n) instead
Confuse :first selector timing with .first() mid-chain
Assume .first() searches the DOM — it only reads the current set
Use .first() when you need the last item — use .last()
Forget that an empty collection stays empty after .first()
Summary
Key Takeaways
Knowledge Unlocked
Five things to remember about .first()
Index zero — one element.
5
Core concepts
1️⃣01
.first()
Index 0
API
002
.eq(0)
Same
Equiv
∅03
Empty
Safe chain
Edge
↩04
.last()
Opposite
Compare
🔗05
Chain
After filter
Pattern
❓ Frequently Asked Questions
.first() reduces the current jQuery collection to the first element in the set — index 0. It returns a new jQuery object containing that single element, or an empty set if the collection was already empty.
No. .first() accepts no parameters. It always selects the element at position zero in the current jQuery object. For other positions, use .eq(n) instead.
They produce the same result — both return the first element as a jQuery object. .first() is a readable shorthand when you always want index zero; .eq(0) is explicit about index-based selection and pairs naturally with .eq(-1) for the last element.
.first() picks the element at index 0. .last() picks the final element in the set — equivalent to .eq(-1). They are complementary shortcuts for the two ends of a collection.
.first() on an empty jQuery object returns another empty jQuery object with length 0. No error is thrown — chained methods simply do nothing.
:first is a jQuery selector pseudo-class used at query time — $('li:first'). .first() is a method on an existing collection — $('li').first(). Prefer .first() when you already have a jQuery object from .filter(), .find(), or another chain step.
Did you know?
jQuery’s .first() and .last() were added together in version 1.4 as readable shortcuts for .eq(0) and .eq(-1). They push onto the internal selection stack like other filtering methods, so you can call .end() afterward to return to the full matched set.