jQuery :nth-child() Selector

Beginner
5 min read
Updated: Nov 2026
3 examples
Selectors

Definition

:nth-child(index) selects elements that are the nth child of their parent. Counting is 1-based (CSS) and includes every sibling element. Available since jQuery 1.1.4.

Syntax

Start with these common calls. Keep them short and readable.

jQuery
$(":nth-child(index | even | odd | equation)")

$("li:nth-child(2)")      → 2nd child if it is an li
$("li:nth-child(even)")   → even child positions
$("li:nth-child(3n)")     → 3, 6, 9…
$("li:nth-child(3n+1)")   → 1, 4, 7…

$("li").eq(1)             → 2nd in the jQuery set (0-based)
$("li:even")              → every other match (ignores parent)
CallWhat it does
Base selectorElement that must match at that child slot
:nth-child(…)Child-position filter
ArgumentIndex, even/odd, or equation

Load jQuery before your script (CDN or local file), then wrap DOM work in $(function () { ... }) when needed.

Parameters

ParameterDescription
Step 1Choose the element type you care about (li, tr, and so on).
Step 2Pass a 1-based index, even/odd, or an An+B equation.
Step 3Remember: all siblings count — not only matching tags.

Return Value

Return ValueDescription
See examplesCheck the examples below for what this API returns in common cases.

jQuery Examples

Example 1 — Official Demo: ul li:nth-child(2)

Mark the second list item in every ul (jQuery API example).

jQuery
$("ul li:nth-child(2)").each(function () {
  this.textContent = this.textContent + " - 2nd!";
});

console.log($("ul li:nth-child(2)").length);
Try It Yourself

How It Works

  1. Scan each list. Every ul has its own child positions.
  2. Require index 2. Only the second child is a candidate.
  3. Require an li. The second child must also be a list item.

Example 2 — Zebra Rows: even / odd

Stripe items by child position inside each parent.

jQuery
console.log("even:", $("ul li:nth-child(even)").length);
console.log("odd:", $("ul li:nth-child(odd)").length);

$("ul li:nth-child(even)").each(function () {
  this.textContent = "[E] " + this.textContent;
});
Try It Yourself

How It Works

  1. Even slots. Positions 2, 4, 6… inside each parent match even.
  2. Odd slots. Positions 1, 3, 5… match odd.
  3. Per parent. Striping restarts in every list or table body.

Example 3 — Equations: 3n and 3n+1

Use An+B patterns from the official playground demo.

jQuery
console.log("3n:", $("ul li:nth-child(3n)").map(function () {
  return this.textContent.trim();
}).get().join(", "));

console.log("3n+1:", $("ul li:nth-child(3n+1)").map(function () {
  return this.textContent.trim();
}).get().join(", "));
Try It Yourself

How It Works

  1. Read An+B. For integer n ≥ 0, compute positions that match.
  2. 3n. Positions 3, 6, 9… (every third child).
  3. 3n+1. Positions 1, 4, 7… (every third, starting at first).

Frequently Asked Questions

:nth-child(index) selects elements that are the nth child of their parent. Counting starts at 1 and includes every sibling element. $("li:nth-child(2)") matches an li only when it is the second child overall.
1-based, per the CSS specification. The first child is :nth-child(1). jQuery methods like .eq() and .first() use 0-based indexing instead.
:nth-child(n) checks child position within each parent and can match one element per parent. .eq(n) picks a single item from the current jQuery collection using a 0-based index, ignoring parent relationships.
:nth-child(even) / :nth-child(odd) stripe by child position inside each parent. Equations like :nth-child(3n) match every 3rd child (3, 6, 9…); :nth-child(3n+1) matches 1, 4, 7…
Yes. Both match elements that are the first child of their parent. Use :nth-child() when you need other positions or equations.
:nth-child(even) counts sibling position within each parent. jQuery's :even filters the flat matched set every other item, with no regard for parent. They can select different nodes.

Next: :nth-last-child() Selector

Count from the end of each parent with the :nth-last-child() selector.

:nth-last-child() Selector tutorial →

CODETOFUN NEWS

Stay Ahead in Tech

Get the latest IT news, programming updates, and technology trends directly on your phone.

Your daily dose of tech, straight to your feed.

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.

10 people found this page helpful