jQuery :first-of-type Selector

Beginner
5 min read
Updated: Nov 2026
3 examples
Selectors

Definition

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

Syntax

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

jQuery
// 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");
CallWhat it does
:first-of-typeLearn jQuery :first-of-type: select the first sibling of each tag name, compare with :first-child and :nth-of-type(1), and mark first spans or paragraphs in each parent.

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

Parameters

ParameterDescription
Step 1Write a typed selector such as $("span:first-of-type").
Step 2Optionally scope: $("div.group span:first-of-type").
Step 3Chain methods — .text(), .addClass(), events.

Return Value

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

jQuery Examples

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.

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.

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.

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() 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.

16 people found this page helpful