jQuery :nth-of-type() Selector

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

What You’ll Learn

The :nth-of-type() selector matches elements by forward position among same-tag siblings — the general form of :first-of-type. Available since jQuery 1.9; counting is 1-based. Pick the second span, stripe every other paragraph, or mark group starts with 3n+1 — even when headings sit between blocks.

01

Syntax

:nth-of-type(n)

02

By tag

Same type only

03

1-based

First = 1

04

Official

span:nth-of-type(2)

05

vs child

Mixed tags

06

An+B

even, 3n+1

Introduction

After learning :first-of-type and :last-of-type, you often need positions in between — the second span in a row of names, every other paragraph in an article, or the first item in each repeating group. CSS provides :nth-of-type() for that, and jQuery has supported it since version 1.9.

The selector counts only among siblings that share the same element name (tag). A heading or paragraph of a different type does not shift the index for spans. Official docs describe it as the nth child in relation to siblings with the same element name — still 1-indexed per the CSS specification.

Understanding the :nth-of-type() Selector

Read span:nth-of-type(2) as: “Among all span siblings under this parent, select the second one.” Other tags in the same parent are ignored for counting purposes.

  • span:nth-of-type(1) → same as :first-of-type for spans.
  • span:nth-of-type(2) → second span (Kim, Ann, Richard in the official demo).
  • p:nth-of-type(even) → even-position paragraphs among p only.
  • li:nth-of-type(3n+1) → 1st, 4th, 7th list items of type li.
💡
Beginner Tip

Always include the tag in the selector — p:nth-of-type(2), not bare :nth-of-type(2). The element name defines which sibling group gets counted.

📝 Syntax

Official jQuery API form (since 1.9):

jQuery
jQuery( ":nth-of-type(index/even/odd/equation)" )

// fixed forward index among same tag (1-based):
$( "span:nth-of-type(2)" )
$( "p:nth-of-type(1)" )       // = :first-of-type for p

// keywords and equations (same-type siblings only):
$( "p:nth-of-type(even)" )
$( "p:nth-of-type(odd)" )
$( "li:nth-of-type(3n)" )
$( "li:nth-of-type(3n+1)" )

Parameters

  • index — positive integer from the first same-type sibling, even/odd, or an An+B CSS equation.

Return value

  • A jQuery object containing every element matching the tag and nth-of-type rule within its parent.
  • Multiple matches when several parents each contain a qualifying element.

Official jQuery API example

jQuery
// Find each span that is second in relation to its sibling spans
$( "span:nth-of-type(2)" )
  .append( " is 2nd sibling span " )
  .addClass( "nth" );

⚡ Quick Reference

GoalSelector
First of tagp:nth-of-type(1)
Second spanspan:nth-of-type(2)
Even paragraphsp:nth-of-type(even)
Every 3rd lili:nth-of-type(3n)
Counts all child typesUse :nth-child() instead

📋 :nth-of-type() vs :nth-child() and :first-of-type

Type-scoped forward counting vs all-sibling counting — essential on mixed markup.

Of-type
span:nth-of-type(1)

First span only

Nth-child
span:nth-child(1)

Must be 1st child

:first-of-type
p:first-of-type

= nth-of-type(1)

.eq(1)
$("span").eq(1)

0-based flat set

Examples Gallery

Example 1 follows the official jQuery demo. Examples 2–5 cover first-of-type equivalence, the difference from :nth-child(), paragraph stripes, and 3n+1 group patterns. Use Try-it links to run each snippet live.

📚 Official jQuery Demo

Highlight the second span among sibling spans in each container.

Example 1 — Official Demo: span:nth-of-type(2)

Append is 2nd sibling span and add class nth to every second span — Kim, Ann, and Richard in the official markup.

jQuery
$( "span:nth-of-type(2)" )
  .append( " is 2nd sibling span " )
  .addClass( "nth" );
Try It Yourself

How It Works

Only span siblings participate in the count. Index 2 means the second span in each container — not the second child overall.

📈 Type-Based Forward Patterns

First-of-type shorthand, mixed markup, stripes, and equations.

Example 2 — First of Type: p:nth-of-type(1)

Equivalent to :first-of-type — selects the first paragraph even when an h3 appears before other paragraphs.

jQuery
$( "p:nth-of-type(1)" ).addClass( "lead" );

// Same as:
// $( "p:first-of-type" ).addClass( "lead" );
Try It Yourself

How It Works

:nth-of-type(1) is the general form; :first-of-type is the readable shorthand for the common case.

Example 3 — Mixed Tags: nth-of-type(1) vs nth-child(1)

When a p is the first child, only the of-type selector matches the first span.

jQuery
$( "span:nth-of-type(1)" ).addClass( "of-type" );   // Span A

$( "span:nth-child(1)" ).addClass( "of-child" );     // no match

// Intro p is first child — span fails nth-child(1)
Try It Yourself

How It Works

This is why articles with intro paragraphs use p:nth-of-type() rather than p:nth-child() for body text patterns.

Example 4 — Paragraph Stripes: p:nth-of-type(even)

Highlight even-position paragraphs; an h3 between them does not reset or skew the count.

jQuery
$( "p:nth-of-type(even)" ).css( "background", "#dbeafe" );

// Paragraphs 2 and 4 highlighted — h3 ignored in p index
Try It Yourself

How It Works

even is shorthand for 2n among same-type siblings — ideal for readable article layouts with mixed headings.

Example 5 — Group Starts: li:nth-of-type(3n+1)

Mark the first item in each group of three list items — positions 1, 4, 7 among li siblings only.

jQuery
$( "li:nth-of-type(3n+1)" ).addClass( "lead" );

// An+B syntax — same grammar as :nth-child() but type-scoped
Try It Yourself

How It Works

The An+B formula generates a sequence. With pure li lists it often matches :nth-child(3n+1); with mixed tags inside the list, of-type is safer.

🚀 Common Use Cases

  • Second labelspan:nth-of-type(2) in name lists (official demo).
  • Lead paragraphp:nth-of-type(1) after a section heading.
  • Article stripesp:nth-of-type(even) with headings between blocks.
  • Grouped listsli:nth-of-type(3n+1) for cycle starts.
  • Table columnstd:nth-of-type(2) for second column cells by type.
  • Mixed markup — prefer of-type over nth-child when sibling tags differ.

🧠 How jQuery Evaluates :nth-of-type()

1

Find candidates

Gather elements matching the tag — e.g. all span or p in scope.

Query
2

Group by type

Within each parent, count only siblings sharing the same element name.

Filter
3

Test formula

Keep elements whose 1-based type index satisfies the number, even/odd, or An+B rule.

Match
4

Return collection

Matching elements become a jQuery object — often one per parent container.

📝 Notes

  • Available since jQuery 1.9 — follows the W3C CSS specification strictly.
  • Counting is 1-indexed among same-type siblings.
  • :first-of-type equals :nth-of-type(1) for a given tag.
  • Only siblings with the same tag name participate — unlike :nth-child().
  • Class names and IDs do not affect of-type counting — only element names matter.
  • Native querySelectorAll("span:nth-of-type(2)") uses identical syntax in modern browsers.

Browser Support

The :nth-of-type() pseudo-class is standard CSS3 and works in jQuery 1.9+. An+B equations and even/odd are supported in evergreen browsers via native querySelectorAll.

CSS3 · jQuery 1.9+

jQuery :nth-of-type() Selector

Supported in all modern browsers. Native: document.querySelectorAll("p:nth-of-type(even)"). Forward 1-based indexing among same-tag siblings.

100% Standard + jQuery
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
:nth-of-type() Universal

Bottom line: Safe for modern apps. Use of-type when sibling tags differ; pair with :nth-last-of-type() for reverse patterns.

Conclusion

The :nth-of-type() selector is the forward, type-scoped counterpart to :first-of-type and :last-of-type. The official span:nth-of-type(2) demo shows how each parent is evaluated independently for the second span among span siblings.

Remember :nth-of-type(1) = :first-of-type, prefer it over :nth-child() when headings or other tags sit between repeated elements, and explore An+B patterns for grouped layouts.

💡 Best Practices

✅ Do

  • Prefix with tag: p:nth-of-type(2)
  • Use on mixed markup where nth-child fails
  • Scope: article p:nth-of-type(even)
  • Start with :first-of-type then generalize to nth-of-type
  • Reuse selectors in CSS and jQuery

❌ Don’t

  • Confuse with :nth-child() on mixed content
  • Use bare $(":nth-of-type(2)") without a tag
  • Assume classes change of-type index — only tags count
  • Swap in .eq(1) expecting parent-aware matching
  • Forget that each parent evaluates its own type group

Key Takeaways

Knowledge Unlocked

Five things to remember about :nth-of-type()

Forward index among same-tag siblings only.

5
Core concepts
2 02

Official

2nd span

Demo
= 03

:first-of-type

nth-of-type(1)

Equiv
04

≠ nth-child

Mixed tags

Rule
3n 05

An+B

Group patterns

Formula

❓ Frequently Asked Questions

:nth-of-type(index) selects every element that is the nth sibling of its tag name under the same parent. $("span:nth-of-type(2)") matches the second span among span siblings — Kim, Ann, and Richard in the official demo — regardless of other tag types nearby.
:nth-child() counts among all sibling element types. :nth-of-type() counts only among siblings with the same tag. A leading p before spans means span:nth-child(1) fails but span:nth-of-type(1) still matches the first span.
Yes. :first-of-type is equivalent to :nth-of-type(1) for a given element type. Both select the first element of that tag name among siblings under the same parent.
Yes. jQuery follows the CSS specification: counting starts at 1. This differs from jQuery methods like .eq() and .first(), which use 0-based indexing on a flat matched collection.
Official jQuery docs support :nth-of-type(even), :nth-of-type(odd), :nth-of-type(3n), and :nth-of-type(3n+1) — evaluated among same-type siblings only, so headings or other tags between paragraphs do not shift paragraph indexes.
jQuery 1.9 added :nth-of-type() alongside :first-of-type, :last-of-type, and related structural pseudo-classes. It works in native querySelectorAll in modern browsers.
Did you know?

The official jQuery span:nth-of-type(2) demo matches Kim, Ann, and Richard — one second-span per parent div — even though each container has a different total number of spans. That is the power of per-parent, type-scoped indexing.

Continue to :last-of-type Selector

After forward type indexes, learn how to match the last element of each tag name among siblings.

:last-of-type 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