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
Fundamentals
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.
Concept
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.
Foundation
📝 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" );
Cheat Sheet
⚡ Quick Reference
Parent markup
button:only-of-type
Why
<div><button></button></div>
Matches
Single button
<div><button></button><button></button></div>
No match
Two button siblings
<div><button></button><span></span></div>
Matches
Span is different tag
<div><button></button><span></span></div>
button:only-child fails
Span is element sibling
Equivalent intersection
p:first-of-type:last-of-type
Same matched set
Compare
📋 :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
Hands-On
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.
Solo summary paragraph → larger readable text
Multi-paragraph article → no only-of-type match
How It Works
An h2 before and after does not block p:only-of-type — only other p elements would.
Applications
🚀 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.
Important
📝 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.
Compatibility
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.
Bottom line: Use p:only-of-type or button:only-of-type — scope to parents; prefer over bare :only-of-type.
Wrap Up
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.
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
Summary
Key Takeaways
Knowledge Unlocked
Five things to remember about :only-of-type
Only same-tag sibling under the parent.
5
Core concepts
tag01
:only-of-type
Tag scope
API
≠02
Not child
Other tags OK
Compare
1st+last03
Equivalent
Of-type both
Tip
2×04
Same tag
No match
Rule
demo05
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.