jQuery :only-of-type Selector

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

What You’ll Learn

The :only-of-type selector matches every element that is the only sibling of its tag name under the same parent. Standard CSS since jQuery 1.9; ideal for lone buttons beside other tags, single paragraphs in mixed blocks, and type-scoped empty states.

01

Syntax

button:only-of-type

02

Tag scope

Same tag only

03

Official

button:only-of-type

04

vs child

Other tags OK

05

1st + last

Of-type both

06

Standard CSS

Native support

Introduction

Mixed markup often contains exactly one <button>, one <p>, or one <li> of a given type — even when other element types share the same parent. jQuery’s :only-of-type pseudo-class finds those elements by checking whether any same-tag sibling exists.

Official jQuery documentation states: if the parent has other child elements with the same element name, nothing is matched. Two buttons in one div disqualify both — but a single button beside two span elements still matches button:only-of-type, which is the key difference from :only-child.

Understanding the :only-of-type Selector

Think of :only-of-type as the type-scoped intersection of first and last among same-tag siblings:

  • button:only-of-type → the sole button in its parent, even if spans follow.
  • Two span siblings → neither matches span:only-of-type.
  • One p after an h2 → matches p:only-of-type but not p:only-child if the heading is an element sibling.
  • Can match multiple elements document-wide — one per qualifying parent.
💡
Beginner Tip

Use :only-child when the element must be alone with no element siblings at all. Use :only-of-type when you care about being the only tag of that kind — the official demo highlights this with buttons beside spans.

📝 Syntax

Official jQuery API form (since 1.9):

jQuery
jQuery( ":only-of-type" )

// typical usage — chain after a type selector:
$( "button:only-of-type" )
$( "p:only-of-type" )
$( "article li:only-of-type" )

Parameters

  • No arguments — keeps elements with no same-tag siblings under the same parent.

Return value

  • A jQuery object containing every matching element (zero or more).
  • Empty collection when every parent has zero or multiple elements of the selected tag.

Official jQuery API example

jQuery
// Label and border each button that is the only button in its parent
$( "button:only-of-type" )
  .text( "Alone" )
  .css( "border", "2px blue solid" );

⚡ Quick Reference

Parent markupbutton:only-of-typeWhy
<div><button></button></div>MatchesSingle button
<div><button></button><button></button></div>No matchTwo button siblings
<div><button></button><span></span></div>MatchesSpan is different tag
<div><button></button><span></span></div>button:only-child failsSpan is element sibling
Equivalent intersectionp:first-of-type:last-of-typeSame matched set

📋 :only-of-type vs :only-child and :first-of-type

Same-tag siblings only vs all element siblings.

:only-of-type
button:only-of-type

No same-tag siblings

:only-child
button:only-child

No element siblings

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

First of tag, may have peers

:last-of-type
p:last-of-type

Last of tag, may have peers

Examples Gallery

Example 1 follows the official jQuery button:only-of-type demo. Examples 2–5 contrast with :only-child, verify :first-of-type:last-of-type, reject duplicate same-tag siblings, and style lone paragraphs in articles.

📚 Official jQuery Demo

Rename and border buttons that are the only button in their parent — spans do not block a match.

Example 1 — Official Demo: button:only-of-type

Official jQuery demo — three lone buttons become “Alone” with a 2px blue border; divs with two buttons stay unchanged.

jQuery
$( "button:only-of-type" )
  .text( "Alone" )
  .css( "border", "2px blue solid" );
Try It Yourself

How It Works

Official markup: a button beside two spans still matches because only button siblings count — not span elements.

Example 2 — button:only-of-type vs button:only-child

Show why a lone button beside spans matches :only-of-type but not :only-child.

jQuery
$( "button:only-of-type" ).css( "backgroundColor", "#dbeafe" );
$( "button:only-child" ).css( "color", "red" );

console.log( "only-of-type →", $( "button:only-of-type" ).length );
console.log( "only-child →", $( "button:only-child" ).length );
Try It Yourself

How It Works

:only-child rejects element siblings of any tag; :only-of-type ignores siblings of other tag names.

📈 Practical Patterns

Equivalence, rejection rules, and article layouts.

Example 3 — Equivalence: p:first-of-type:last-of-type

When exactly one paragraph exists among siblings, both selectors return the same count.

jQuery
console.log( ":only-of-type →", $( "p:only-of-type" ).length );
console.log( ":first-of-type:last-of-type →", $( "p:first-of-type:last-of-type" ).length );
Try It Yourself

How It Works

A single p among headings is simultaneously first and last of type — shorthand: :only-of-type.

Example 4 — Same-Tag Rejection: span:only-of-type

Official rule — two spans in one parent means neither matches.

jQuery
$( "span:only-of-type" ).css( "fontWeight", "bold" );

console.log( "Matches →", $( "span:only-of-type" ).length );
// 1 from solo span div, 0 from two-span div
Try It Yourself

How It Works

Official docs: other child elements with the same element name prevent a match — duplicate tags fail.

Example 5 — Article Body: article p:only-of-type

Emphasize a single body paragraph when headings surround it.

jQuery
$( "article p:only-of-type" ).css({
  fontSize: "1.125rem",
  lineHeight: "1.7"
});
Try It Yourself

How It Works

An h2 before and after does not block p:only-of-type — only other p elements would.

🚀 Common Use Cases

  • Action bars — style button:only-of-type as a full-width CTA beside icons.
  • Article summaries — detect p:only-of-type for lead-paragraph typography.
  • Form rows — widen a lone input:only-of-type in a mixed label row.
  • List badges — highlight li:only-of-type in compact menus.
  • Validation — flag parents with exactly one element of a required tag.
  • CSS parity — reuse :only-of-type in stylesheets and jQuery.

🧠 How jQuery Evaluates :only-of-type

1

Parse selector

Combine type selector with :only-of-type — e.g. button:only-of-type.

Query
2

Count same-tag siblings

Among element children of the parent, count nodes with the same tag name.

CSS
3

Require exactly one

Keep the element only if it is the sole sibling of that tag — other tag names are ignored.

Filter
4

Return collection

Chain .text(), .css(), or other jQuery methods.

📝 Notes

  • Available since jQuery 1.9 — standard CSS pseudo-class.
  • Official rule: other child elements with the same element name prevent a match.
  • Siblings of other tag names do not block — key difference from :only-child.
  • Can match multiple elements — one per qualifying parent.
  • Works in native querySelectorAll — no jQuery extension caveat.
  • Shorthand mental model: :first-of-type:last-of-type for a given tag.

Browser Support

The :only-of-type pseudo-class is standard CSS and works in jQuery 1.9+. All modern browsers support document.querySelectorAll("p:only-of-type"). Same syntax in stylesheets and jQuery selectors.

CSS · jQuery 1.9+

jQuery :only-of-type Selector

Universal browser support. Native: querySelectorAll("button:only-of-type").

100% Standard CSS
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
:only-of-type Universal

Bottom line: Use p:only-of-type or button:only-of-type — scope to parents; prefer over bare :only-of-type.

Conclusion

The :only-of-type selector matches every element that is the only sibling of its tag name under the same parent — the official button:only-of-type demo labels three lone buttons “Alone” with a blue border.

Distinguish it from :only-child, remember the same-tag sibling rule, and reuse the selector in CSS when possible.

💡 Best Practices

✅ Do

  • Use article p:only-of-type for scoped queries
  • Choose :only-of-type when tag uniqueness matters
  • Compare with :only-child when mixed tags appear
  • Remember duplicate same-tag siblings fail the match
  • Reuse the same selector in CSS and jQuery

❌ Don’t

  • Confuse :only-of-type with :only-child without reading docs
  • Expect a match when two elements share the same tag
  • Use bare $(":only-of-type") on large pages
  • Assume other tag siblings block the match
  • Replace :only-of-type with :only-child on mixed markup

Key Takeaways

Knowledge Unlocked

Five things to remember about :only-of-type

Only same-tag sibling under the parent.

5
Core concepts
02

Not child

Other tags OK

Compare
1st+last 03

Equivalent

Of-type both

Tip
04

Same tag

No match

Rule
demo 05

Official

Alone btn

Demo

❓ Frequently Asked Questions

:only-of-type selects every element that has no siblings with the same element name (tag) under the same parent. $("button:only-of-type") matches the sole button in a div even when span siblings exist. Available since jQuery 1.9.
Nothing matches for that parent. Official jQuery docs: if the parent has other child elements with the same element name, :only-of-type matches nothing. Two buttons in one div means neither button matches button:only-of-type.
:only-child requires no element siblings at all. :only-of-type only compares siblings with the same tag — a lone button beside span siblings matches button:only-of-type but not button:only-child.
Yes. When exactly one element of a tag exists among siblings, it is both :first-of-type and :last-of-type — so p:first-of-type:last-of-type matches the same nodes as p:only-of-type in practice.
Yes. :only-of-type is standard CSS and works in native querySelectorAll — e.g. document.querySelectorAll("p:only-of-type"). jQuery has supported it since 1.9.
Yes — $("p:only-of-type") or $("article h2:only-of-type") is clearer than bare $(":only-of-type"). Scoping to a parent class or ID avoids matching unintended lone elements across the page.
Did you know?

The official jQuery :only-of-type demo uses the same purple floated div layout as :only-child, but matches three buttons — including one beside two span siblings — because only same-tag siblings matter for this selector.

Continue to Descendant Selector

After structural of-type patterns, learn how the space combinator matches nested elements at any depth.

Descendant selector 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