jQuery :eq() Selector

Beginner
⏱️ 9 min read
📚 Updated: Jul 2026
🎯 5 Examples
🚀 5 Try-it labs
Index filter

What You’ll Learn

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

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.

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.

📝 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" );

⚡ Quick Reference

Goal:eq() (0-based):nth-child() (1-based CSS)
First match:eq(0):nth-child(1)
Third match in settd:eq(2)N/A — different model
Second child of each parentOne global index onlyli:nth-child(2)
Last element:eq(-1):last-child
Modern jQuery 3.4+DeprecatedUse .eq(n) method

📋 :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

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.

Example 1 — Official Demo: td:eq(2)

Find the third td and set its text color to red.

jQuery
$( "td:eq( 2 )" ).css( "color", "red" );



// TD #0, #1 unchanged — TD #2 (third cell) turns red
Try It Yourself

How It Works

All td elements are collected first, then :eq(2) keeps only index 2 — the third item in a 0-based list.

Example 2 — Single Match: ul.nav li:eq(1)

Official demo — yellow background on exactly one list item at global index 1.

jQuery
$( "ul.nav li:eq(1)" ).css( "backgroundColor", "#ff0" );



// Only ONE li gets yellow — not every second item
Try It Yourself

How It Works

:eq() is designed to select a single element from the combined matched set — unlike :nth-child(2), which can match multiple elements.

Example 3 — Negative Index: li:eq(-2)

Official demo — add class foo to the second-to-last li.

jQuery
$( "li:eq(-2)" ).addClass( "foo" );



// Targets "List 2, item 2" when six li elements exist
Try It Yourself

How It Works

Since jQuery 1.8, negative values count backwards. :eq(-1) is the last element; :eq(-2) is one before that.

📈 Practical Patterns

Modern API, per-container indexing, and CSS comparison.

Example 4 — Modern Pattern: $(".box").eq(2)

jQuery 3.4+ — use the method instead of the deprecated pseudo-class.

jQuery
// Recommended — pure CSS selector + .eq()

$( ".box" ).eq( 2 ).addClass( "picked" );



// Avoid in new code:

// $( ".box:eq(2)" )
Try It Yourself

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 each ul.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" );
Try It Yourself

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.

🚀 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.

📝 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.

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 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
:eq() Universal

Bottom line: Learn :eq() for legacy code; write new code with .eq() after a pure CSS selector.

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.

💡 Best Practices

✅ Do

  • Use $("td").eq(2) in new jQuery 3.4+ code
  • Scope with a class or ID before indexing
  • Use .each() + scoped :eq(n) per container
  • Remember 0-based indices (:eq(0) = first)
  • 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

Key Takeaways

Knowledge Unlocked

Five things to remember about :eq()

One element by index in the matched set.

5
Core concepts
1 02

Single

One match

Rule
03

Negative

From end

1.8+
.eq 04

Modern

Not deprecated

Tip
demo 05

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.

Continue to :lt() Selector

After single-index picks, learn how :lt() selects every element before a cutoff index.

:lt() 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