: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.
Remember
$(":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)
Attach the pseudo-class to a tag or class, then pass an index, keyword, or equation.
Choose the element type you care about (li, tr, and so on).
Pass a 1-based index, even/odd, or an An+B equation.
Remember: all siblings count — not only matching tags.
Do not confuse with .eq() (0-based, flat collection).
Piece
Required?
Role
Base selector
Usually
Element that must match at that child slot
:nth-child(…)
Yes
Child-position filter
Argument
Yes
Index, even/odd, or equation
Pseudocode
Pseudocode
for each element E matching base:
parent = E.parent
index = 1-based position of E among parent's children
if index matches (n | even | odd | An+B):
keep E
return jQuery(kept)
Reference
Cheat Sheet
Keep these facts handy while writing child-index selectors.
Topic
Detail
Syntax
$(":nth-child(n | even | odd | An+B)")
Counting
1-based (CSS); all siblings count
even / odd
By child position inside each parent
3n / 3n+1
Every 3rd; every 3rd starting at 1
vs .eq()
Parent child slot vs flat 0-based pick
vs :even
Parent-aware vs set-aware
Since
jQuery 1.1.4 (CSS)
Try it
Live Preview
Pick an :nth-child() pattern against two short lists and see which items match.
Two lists — matches can appear once per parent, like the official demo.
John
Karl
Brandon
Sam
Glen
Tane
Ralph
David
Result
$("ul li:nth-child(2)")
matched: 2
text: Karl, Tane
/* 1-based child position per parent */
Code
jQuery Examples
Short, copy-ready snippets. Open a browser console with jQuery loaded, or use Try It Yourself for a live editor.
Example 1 — Official Demo: ul li:nth-child(2)
Mark the second list item in every ul (jQuery API example).
3n: Brandon, Ralph
3n+1: John, Sam, Glen, David
3n → 3, 6… · 3n+1 → 1, 4, 7…
How It Works
Read An+B. For integer n ≥ 0, compute positions that match.
3n. Positions 3, 6, 9… (every third child).
3n+1. Positions 1, 4, 7… (every third, starting at first).
Same idea as CSS. jQuery follows the CSS nth formula.
Example 4 — :nth-child() vs .eq()
Same number, different meaning — parent slot vs flat collection index.
jQuery
var nth = $("ul li:nth-child(2)").map(function () {
return this.textContent.trim();
}).get().join(", ");
var eq = $("ul li").eq(1).text().trim();
console.log("nth-child(2):", nth); // one per list
console.log(".eq(1):", eq); // one item, 0-based
nth-child(2): Karl, Tane
.eq(1): Karl
Official docs: counting rules differ on purpose
How It Works
Child position.:nth-child(2) is 1-based and parent-aware.
Set position..eq(1) is 0-based inside the matched set.
Multiple parents. nth can return several; eq returns one.
Rule of thumb. Tree position → nth. Flat pick → .eq() / :eq().
Example 5 — :nth-child(even) vs :even
Parent-aware striping is not the same as filtering the flat match list.
jQuery
console.log("nth even:", $("ul li:nth-child(even)").length);
console.log(":even:", $("ul li:even").length);
// Official note: :even ignores parent and filters the list;
// :nth-child(even) counts child index per parent.
One line:$("li:nth-child(n)") means “the nth child of its parent — if that child is an li.”
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.