jQuery :first-of-type Selector

Beginner
6 min read
Updated: Oct 2026
5 examples
Live preview

What Is :first-of-type?

:first-of-type (since jQuery 1.9) selects every element that is the first among siblings of the same tag name. Unlike :first-child, an earlier sibling of a different tag does not block the match. It is equivalent to :nth-of-type(1).

Remember
// Official pattern — first span under each parent
$("span:first-of-type");

// First paragraph in each article
$("article p:first-of-type");

// Same as :nth-of-type(1)
$("span:nth-of-type(1)");

// Different from :first-child (any tag must be #1)
$("span:first-child");
NeedUse
First of a tag among siblings$("span:first-of-type")
First child of any tag$("span:first-child")
Nth of a tag$("span:nth-of-type(2)")
Last of a tag$("span:last-of-type")
Only one of that tag$("span:only-of-type")

How to Use :first-of-type

Pick a tag, optionally scope a parent, then style or read the first matching sibling of that type.

  1. Write a typed selector such as $("span:first-of-type").
  2. Optionally scope: $("div.group span:first-of-type").
  3. Chain methods — .text(), .addClass(), events.
  4. Remember: only same-tag siblings matter for this filter.
  5. Use :first-child when the element must be child #1 of any type.
PieceRequired?Role
Tag nameRecommendede.g. span, p, h2
:first-of-typeYesFirst sibling with that tag
Parent scopeOptionalLimits which trees are searched

Pseudocode

Pseudocode
for each element matching the left side (e.g. span):
    look at its parent and same-tag siblings
    if no earlier sibling has the same tag name:
        include it in the result

result may contain many elements
    → one match is possible for each parent × tag

.compare with :first-child:
    first-child → must be the first element child (any tag)
    first-of-type → must be the first of this tag only

Cheat Sheet

Keep these facts handy while selecting first-of-type elements.

TopicDetail
Selector$("elem:first-of-type")
SincejQuery 1.9
Matches0–many (one per parent × tag possible)
Equivalent:nth-of-type(1)
vs :first-childSame-tag first vs absolute first child
CSS?Yes — standard CSS pseudo-class
Official demo$("span:first-of-type")

Live Preview

Mark first-of-type spans, compare with :first-child, or count matches.

Sample: two groups. Group B starts with em, so span:first-child fails there while span:first-of-type still matches.

Corey, Yehuda, Adam
Note: Nobody, Jörn, Scott
Result
Ready — pick an action and click Run.

jQuery Examples

Short, copy-ready snippets. Prefer a typed selector over bare $(":first-of-type").

Example 1 — Official span:first-of-type

Mark the first span in each div — the documented pattern.

jQuery
$("span:first-of-type").each(function () {
  $(this).text($(this).text() + " [fot]");
});
Try It Yourself

How It Works

  1. Select. span:first-of-type finds first spans only.
  2. Per parent. Each div contributes one match.
  3. Mark. Append [fot] to those spans.
  4. Skip. Later same-tag siblings stay unchanged.

Example 2 — :first-of-type vs :first-child

When another tag comes first, only first-of-type still matches the span.

jQuery
var fot = $("div span:first-of-type").length;
var child = $("div span:first-child").length;

$("#out").text(
  "span:first-of-type → " + fot +
  ", span:first-child → " + child
);
Try It Yourself

How It Works

  1. Setup. An em is the absolute first child.
  2. of-type. The first span still matches.
  3. child. That span is not child #1 → no match.
  4. Choose. Pick the pseudo-class that matches your intent.

Example 3 — Scoped Parent

Limit matches to a specific container.

jQuery
$("#team span:first-of-type").prepend("* ");
Try It Yourself

How It Works

  1. Scope. #team limits the search tree.
  2. Filter. Only first-of-type spans inside it match.
  3. Mark. Prepend * to those spans.
  4. Outside. Other parents are ignored.

Example 4 — Same as :nth-of-type(1)

Both forms select the first sibling of that tag.

jQuery
var a = $("div span:first-of-type").map(function () {
  return $(this).text();
}).get().join(", ");

var b = $("div span:nth-of-type(1)").map(function () {
  return $(this).text();
}).get().join(", ");

$("#out").text("fot=[" + a + "] nth=[" + b + "]");
Try It Yourself

How It Works

  1. Run both. Collect text from each selector.
  2. Compare. Results match for the same markup.
  3. Index. Type indexes are 1-based in CSS.
  4. Pick. :first-of-type usually reads clearer.

Example 5 — First Paragraph Amid Mixed Tags

Grab the first p even when a heading comes before it.

jQuery
$("article p:first-of-type").prepend("[lead] ");
$("#out").text($("article p:first-of-type").text());
Try It Yourself

How It Works

  1. Markup. An h2 is the first child.
  2. Select. p:first-of-type finds the first paragraph.
  3. Mark. Prepend a lead label.
  4. Note. p:first-child would fail here.

Edge Cases & Pitfalls

These mix-ups cause most :first-of-type surprises.

vs child

Not the same as :first-child

Use :first-of-type for “first span/p/h2”. Use :first-child for “must be child #1”.

tag

Tag name matters

span and p each have their own first-of-type under the same parent.

scope

Bare $(":first-of-type") is broad

Prefix with a tag and parent scope to avoid matching the whole document.

version

Needs jQuery 1.9+

Older 1.x builds before 1.9 do not include this selector.

Browser Support

:first-of-type is a standard CSS pseudo-class. jQuery has supported it since 1.9. Works wherever your loaded jQuery build (and the browser CSS engine) runs.

✓ Stable · jQuery 1.9+

jQuery :first-of-type

Universal CSS / jQuery support on modern stacks. Prefer current jQuery 3.x+ for consistent selector engines.

100% CSS + 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
:first-of-type Universal

Bottom line: Safe for everyday UI. Prefer typed, scoped selectors over bare :first-of-type.

Performance Notes

ApproachCostBest for
Scoped $("#main p:first-of-type")CheapTypical UI highlighting
Bare $(":first-of-type")BroaderAvoid on large pages
:first-child when you mean absolute firstCheapStricter position checks

Modern browsers implement :first-of-type in native CSS. Keep selectors scoped so jQuery does less work.

Key Takeaways

  • Role: first sibling of a given tag name under each parent.
  • vs child: ignores earlier siblings of other tags.
  • Equals: :nth-of-type(1).
  • Scope: prefer typed selectors like $("p:first-of-type").

One line: :first-of-type finds the first sibling of a tag — use :first-child when position #1 among all tags is required.

Frequently Asked Questions

:first-of-type selects every element that is the first among siblings with the same tag name. $("span:first-of-type") matches the first span under each parent. Available since jQuery 1.9.
:first-child requires the element to be the first child of any tag. :first-of-type only compares same-tag siblings. If a <p> comes before a <span>, the span is not :first-child but can still be span:first-of-type.
Yes. In CSS, :first-of-type is equivalent to :nth-of-type(1).
Yes. It is a standard CSS pseudo-class and works with native querySelectorAll. jQuery has supported it since 1.9.
Another element type may be the first child. Example: <div><p>Intro</p><span>Lead</span></div> — the span matches span:first-of-type but not span:first-child.
Yes — $("p:first-of-type") or $("article h2:first-of-type") is clearer than bare $(":first-of-type"). Scope to a parent when possible.

Next: :nth-of-type() Selector

Select the nth sibling of a given tag — including formulas like 2n.

:nth-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.

16 people found this page helpful