jQuery :only-of-type Selector

Beginner
5 min read
Updated: Nov 2026
3 examples
Selectors

Definition

:only-of-type selects every element that has no siblings with the same element name under its parent. Official docs: if the parent has other children with the same tag, nothing is matched. Other tags are allowed. Available since jQuery 1.9. Standard CSS.

Syntax

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

jQuery
$(":only-of-type")
$("button:only-of-type")   → sole button (spans OK)
$("p:only-of-type")        → sole p among siblings

<div><button></button></div>                    → matches
<div><button></button><span></span></div>      → matches
<div><button></button><button></button></div>  → no match
CallWhat it does
:only-of-typeLearn the jQuery :only-of-type selector: match elements with no same-tag siblings, official button:only-of-type demo, vs :only-child and :first-of-type/:last-of-type, 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 (button, p, and so on).
Step 2Add :only-of-type — e.g. button:only-of-type.
Step 3Confirm no sibling shares that same tag name.

Return Value

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

jQuery Examples

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

Find buttons that are the only button under their parent (jQuery API example).

jQuery
var $hits = $("button:only-of-type");
$hits.text("Alone");

console.log($hits.length);
console.log($hits.map(function () {
  return this.textContent.trim();
}).get().join(", "));
Try It Yourself

How It Works

  1. Match buttons. $("button:only-of-type") tests each button.
  2. Skip duplicates. Box 1 has two buttons — neither stays.
  3. Keep unique buttons. Boxes 2, 4, and 5 each have one button.

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

Other-tag siblings allow of-type but block only-child.

jQuery
var $box = $("#box-spans");
console.log("only-of-type:", $box.find("button:only-of-type").length);
console.log("only-child:", $box.find("button:only-child").length);
console.log("spans:", $box.find("span").length);
Try It Yourself

How It Works

  1. One button. Unique among button siblings → of-type matches.
  2. Two spans. Extra element children exist.
  3. :only-child fails. Any element sibling blocks it.

Example 3 — Same-Tag Siblings Reject

Two elements of the same tag under one parent → neither is only-of-type.

jQuery
var $box = $("#box-pair");
console.log("buttons:", $box.find("button").length);
console.log("only-of-type:", $box.find("button:only-of-type").length);
console.log("first-of-type:", $box.find("button:first-of-type").length);
console.log("last-of-type:", $box.find("button:last-of-type").length);
Try It Yourself

How It Works

  1. Two buttons. Official rule: same element name → no only-of-type match.
  2. First still works. One button is :first-of-type.
  3. Last still works. The other is :last-of-type.

Frequently Asked Questions

:only-of-type selects every element that has no siblings with the same element name 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 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 matches.
:only-child requires no element siblings at all. :only-of-type only compares same-tag siblings — a lone button beside spans matches button:only-of-type but not button:only-child.
Yes in practice. 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.
Yes. :only-of-type is standard CSS and works with native querySelectorAll — e.g. document.querySelectorAll("p:only-of-type"). jQuery has supported it since 1.9.
Yes. Prefer $("button:only-of-type") or $("article p:only-of-type") over bare $(":only-of-type") so the intent and scope stay clear.

Next: Descendant Selector (ancestor descendant)

Select nested matches with the classic ancestor descendant pattern.

Descendant Selector (ancestor descendant) 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.

11 people found this page helpful