jQuery :nth-of-type() Selector

Beginner
5 min read
Updated: Nov 2026
3 examples
Selectors

Definition

:nth-of-type(index) selects elements that are the nth sibling of their tag name under the same parent. Other tags between them do not change the count. Available since jQuery 1.9.

Syntax

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

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

$("span:nth-of-type(1)")     → first span (≡ :first-of-type)
$("span:nth-of-type(2)")     → second span among spans
$("p:nth-of-type(even)")     → even paragraph positions

$("span:nth-child(2)")       → 2nd child among ALL tags
$("span:nth-last-of-type(2)") → 2nd span from the end
CallWhat it does
:nth-of-type()Learn the jQuery :nth-of-type() selector: 1-based index among same-tag siblings, vs :nth-child(), even/odd, An+B, examples, pitfalls, and FAQs.

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

Parameters

ParameterDescription
Step 1Choose the element type (span, p, li, and so on).
Step 2Pass a 1-based index among same-type siblings, or even/odd/An+B.
Step 3Ignore other tag names when counting — they do not shift of-type indexes.

Return Value

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

jQuery Examples

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

Find each span that is second among its sibling spans (jQuery API example).

jQuery
$("span:nth-of-type(2)").each(function () {
  this.textContent = this.textContent + " is 2nd sibling span";
});

console.log($("span:nth-of-type(2)").length);
Try It Yourself

How It Works

  1. Group by parent. Each container numbers its own spans.
  2. Count only spans. Other tags (if any) do not change span indexes.
  3. Keep index 2. The second span in each group matches.

Example 2 — First of Type: (1) ≡ :first-of-type

Show that index 1 is the first element of that tag among siblings.

jQuery
var a = $("span:nth-of-type(1)").map(function () {
  return this.textContent.trim();
}).get().join(", ");

var b = $("span:first-of-type").map(function () {
  return this.textContent.trim();
}).get().join(", ");

console.log(a);
console.log(b);
console.log(a === b);
Try It Yourself

How It Works

  1. Index 1 from the start. That is always the first span in the parent.
  2. Compare with :first-of-type. Same nodes on the same markup.
  3. Prefer the short form. Use :first-of-type when you only need the first of that tag.

Example 3 — Of-Type vs :nth-child()

Mixed tags show why same-type counting matters.

jQuery
// Markup: <p>note</p> <span>A</span> <span>B</span>
console.log("of-type:", $("span:nth-of-type(1)").text());
console.log("nth-child:", $("span:nth-child(1)").length);

// of-type → "A" (first span)
// nth-child → 0 (first child is the p)
Try It Yourself

How It Works

  1. Of-type group. Spans are numbered A=1, B=2 from the start.
  2. First span wins. span:nth-of-type(1) selects A.
  3. All-children check. The first child is p, so span:nth-child(1) fails.

Frequently Asked Questions

:nth-of-type(index) selects elements that are the nth sibling of their tag name under the same parent. $("span:nth-of-type(2)") matches the second span among span siblings — regardless of other tag types nearby.
:nth-child() counts every sibling element. :nth-of-type() counts only 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. Both select the first element of that tag name among siblings under the same parent.
Yes. Per CSS and jQuery docs, counting starts at 1. This differs from .eq(), which uses 0-based indexing on a flat collection.
Official docs support :nth-of-type(even), odd, 3n, and 3n+1 — evaluated among same-type siblings only, so other tags between them do not shift indexes.
jQuery 1.9. It follows the W3C CSS specification and works via querySelectorAll in modern browsers.

Next: :last-of-type Selector

Select the last element of its type among siblings with :last-of-type.

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

9 people found this page helpful