The :eq(index) selector picks one element at a zero-based position within the matched set built by the selector before it. Available since jQuery 1.0; negative indices since 1.8; deprecated in jQuery 3.4 in favor of the .eq() method.
01
Syntax
td:eq(2)
02
0-based
Index 2 = 3rd
03
Official
td:eq(2)
04
Negative
:eq(-2)
05
vs nth
1-based CSS
06
.eq()
Modern API
Fundamentals
Introduction
When you need exactly one element from a group — the third table cell, the second list item in a navigation bar, or the last-but-one row — jQuery’s :eq() pseudo-class filters by position in the current matched set.
Index-related selectors (:eq(), :lt(), :gt(), :even, :odd) always follow another selector. If $(".myclass") returns four elements, they are indexed 0 through 3 for :eq(). Because JavaScript uses 0-based indexing, $(".myclass:eq(1)") selects the second matching element, not the first.
Concept
Understanding the :eq() Selector
Think of :eq() as picking a single slot from a numbered list:
td:eq(0) → first td in the matched set.
td:eq(2) → third td (official demo — red text).
li:eq(-1) → last li (since jQuery 1.8).
li:eq(-2) → second-to-last li (official demo — class foo).
ul.nav li:eq(1) → one global match at index 1 across all nav items.
💡
Beginner Tip
For new code, prefer $("td").eq(2) over $("td:eq(2)"). The .eq() method is not deprecated, works with pure CSS selectors, and can use native querySelectorAll for the first step.
Foundation
📝 Syntax
Official jQuery API forms:
jQuery
jQuery( ":eq(index)" ) // since 1.0 — zero-based
jQuery( ":eq(-index)" ) // since 1.8 — from end
// typical usage — chain after a selector:
$( "td:eq( 2 )" )
$( "ul.nav li:eq(1)" )
$( "li:eq(-2)" )
// modern replacement (jQuery 3.4+):
$( "td" ).eq( 2 )
Parameters
index — zero-based position in the matched set (non-negative since 1.0).
-index — zero-based position counting backwards from the last element (since 1.8).
Return value
A jQuery object containing at most one element.
Empty collection when the index is out of range.
Official jQuery API example
jQuery
// Find the third td (index 2)
$( "td:eq( 2 )" ).css( "color", "red" );
Cheat Sheet
⚡ Quick Reference
Goal
:eq() (0-based)
:nth-child() (1-based CSS)
First match
:eq(0)
:nth-child(1)
Third match in set
td:eq(2)
N/A — different model
Second child of each parent
One global index only
li:nth-child(2)
Last element
:eq(-1)
:last-child
Modern jQuery 3.4+
Deprecated
Use .eq(n) method
Compare
📋 :eq() vs .eq() and :nth-child()
Index filter vs method vs CSS child position.
:eq(n)
li:eq(1)
One match at index 1
.eq(n)
$("li").eq(1)
Recommended since 3.4
:nth-child
li:nth-child(2)
2nd child per parent
.each()
.find("li:eq(1)")
Per-list index
Hands-On
Examples Gallery
Examples 1–3 follow official jQuery demos. Examples 4–5 compare the modern .eq() method and per-container indexing inside .each(). Use the Try-it links to run each snippet.
📚 Official jQuery Demos
Zero-based indexing and negative indices from the API docs.
Third .box (index 2) → picked class
Browser can optimize the CSS query step
How It Works
Official docs: because :eq() is a jQuery extension, it cannot use native querySelectorAll. Splitting into CSS + .eq() improves performance.
Example 5 — Per List: .each() with li:eq(1)
Official demo — italicize the second item in eachul.nav, plus compare with :nth-child(2).
jQuery
$( "ul.nav" ).each( function() {
$( this ).find( "li:eq(1)" ).css( "fontStyle", "italic" );
});
// Contrast — red text on every 2nd child of each ul:
$( "ul.nav li:nth-child(2)" ).css( "color", "red" );
Each ul's 2nd li → italic (via scoped :eq(1))
Both lists' 2nd children → red (via :nth-child(2))
How It Works
Scoped .find("li:eq(1)") resets the matched set to each list’s items, so index 1 is the second item in that list only.
Applications
🚀 Common Use Cases
Table cells — highlight td:eq(2) in a grid row or column slice.
Navigation — style one item at a fixed index in a flat matched set.
Last items — use :eq(-1) or :eq(-2) without counting length.
Per container — combine .each() with scoped :eq(n).
Legacy code — read and maintain existing :eq() selectors in older jQuery projects.
Migration — replace :eq(n) with .eq(n) for jQuery 3.4+ codebases.
🧠 How jQuery Evaluates :eq()
1
Build matched set
Run the selector before :eq() — e.g. all td or ul.nav li.
Query
2
Assign indices
Number elements 0 through length − 1 in document order within the set.
0-based
3
Pick one index
Keep the element at n, or count backwards for negative n.
Filter
4
✓
Return collection
At most one element — ready for chaining .css(), .addClass(), etc.
Important
📝 Notes
Available since jQuery 1.0; negative indices since 1.8.
Deprecated in jQuery 3.4 — prefer .eq(index) method on a jQuery collection.
0-based indexing — :eq(1) is the second element, not the first.
Not valid in native querySelectorAll — jQuery extension only.
Differs from :nth-child(n) — CSS uses 1-based child position per parent.
Index selectors filter the set from preceding expressions — scope matters.
Compatibility
Browser Support
The :eq() pseudo-class is a jQuery extension — not standard CSS — and works in jQuery 1.0+ (negative indices since 1.8). It is deprecated in 3.4+; use .eq() instead. The method works in all browsers that support jQuery.
✓ jQuery 1.0+ · deprecated 3.4
jQuery :eq() Selector
Works in all jQuery versions. Modern code: $("td").eq(2) instead of $("td:eq(2)").
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
:eq()Universal
Bottom line: Learn :eq() for legacy code; write new code with .eq() after a pure CSS selector.
Wrap Up
Conclusion
The :eq(index) selector picks one element at a zero-based position in the matched set — the official td:eq(2) demo highlights the third table cell. Negative indices count from the end since jQuery 1.8.
For new projects on jQuery 3.4+, use .eq() instead. When reading legacy code, remember the difference from :nth-child() and the 0-based index rule.
Use negative indices for last / second-to-last items
❌ Don’t
Add new :eq() selectors in modern codebases
Confuse :eq(1) with :nth-child(1)
Expect ul li:eq(1) to style every second item
Use :eq() without a preceding selector
Assume negative indices work before jQuery 1.8
Summary
Key Takeaways
Knowledge Unlocked
Five things to remember about :eq()
One element by index in the matched set.
5
Core concepts
#01
:eq(n)
0-based
API
102
Single
One match
Rule
−03
Negative
From end
1.8+
.eq04
Modern
Not deprecated
Tip
demo05
Official
td:eq(2)
Demo
❓ Frequently Asked Questions
:eq(index) selects exactly one element at the given zero-based index within the matched set that precedes it. $("td:eq(2)") picks the third td among all td elements in scope. Available since jQuery 1.0; negative indices since 1.8.
It selects the second element. jQuery uses 0-based indexing like JavaScript arrays — :eq(0) is first, :eq(1) is second. CSS :nth-child(n) uses 1-based indexing instead.
Since jQuery 1.8, :eq(-index) counts backwards from the last element. $("li:eq(-2)") matches the second-to-last li in the matched set — official jQuery demo adds class foo to List 2, item 2.
Yes — deprecated in jQuery 3.4. Official docs recommend removing :eq() from selectors and using the .eq(index) method after a pure CSS selector instead: $("td").eq(2) rather than $("td:eq(2)").
:eq() filters the jQuery matched set by position (0-based). :nth-child(n) is CSS — it picks elements that are the nth child of their parent (1-based). Official demo: ul.nav li:eq(1) picks one li globally; li:nth-child(2) picks the second child in each parent.
:eq() always follows a selector that builds the matched set — e.g. $("li:eq(1)") not $(":eq(1)"). Index selectors (:eq, :lt, :gt, :even, :odd) narrow the set produced by what comes before them.
Did you know?
Official jQuery docs demonstrate that $("ul.nav li:eq(1)") returns a single element, while $("ul.nav li:nth-child(2)") can return multiple — one per list. Wrapping :eq() inside .each() gives you per-container indexing without changing the global matched set.