jQuery :only-child Selector

Beginner
5 min read
Updated: Nov 2026
3 examples
Selectors

Definition

:only-child selects every element that is the only element child of its parent — no sibling elements at all. Official docs: if the parent has other child elements, nothing is matched. Available since jQuery 1.1.4. Standard CSS.

Syntax

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

jQuery
$(":only-child")
$("div button:only-child")   → button alone in its div
$("li:only-child")           → sole li in its list

<div><button></button></div>              → matches
<div><button></button><button></button></div> → no match
<div>Sibling! <button></button></div>   → matches (text ignored)
CallWhat it does
:only-childLearn the jQuery :only-child selector: match elements with no element siblings, official div button:only-child demo, vs :only-of-type and :first-child/:last-child, 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, li, and so on).
Step 2Add :only-child — or combine as div button:only-child.
Step 3Confirm the parent has no other element children.

Return Value

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

jQuery Examples

Example 1 — Official Demo: div button:only-child

Find buttons that are the only element child of their div (jQuery API example).

jQuery
var $hits = $("div button:only-child");
$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. Scope to divs. div button finds buttons inside divs.
  2. Keep lone buttons. :only-child drops any button with element siblings.
  3. Three matches. Official boxes 2, 3, and 5 qualify.

Example 2 — Text Nodes Do Not Block

Show that “Sibling!” text still allows a lone button to match.

jQuery
var $box = $("#box-text");
var buttons = $box.children("button").length;
var only = $box.find("button:only-child").length;

console.log("button elements:", buttons);
console.log("only-child matches:", only);
console.log("text present:", $box.text().indexOf("Sibling!") !== -1);
Try It Yourself

How It Works

  1. Count elements. Only one button child exists.
  2. Text is noise. “Sibling!” is a text node, not an element sibling.
  3. Match succeeds. button:only-child still selects it.

Example 3 — Two Buttons → No Match

When two element siblings share a parent, neither is :only-child.

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

How It Works

  1. Two element children. Official rule: nothing matches :only-child.
  2. First still works. One button is :first-child.
  3. Last still works. The other is :last-child.

Frequently Asked Questions

:only-child selects every element that is the only element child of its parent — no sibling elements before or after it. $("div button:only-child") matches buttons whose parent contains just that one button. Available since jQuery 1.1.4.
Nothing matches for that parent. Official docs: if the parent has other child elements, :only-child matches nothing. A button beside another button, span, or div does not match.
No. Structural pseudo-classes count element siblings only. Text like "Sibling!" in the official demo does not block a lone button from matching :only-child.
A sole element child is both :first-child and :last-child. So p:only-child matches the same nodes as p:first-child:last-child when no other element siblings exist.
:only-child requires no element siblings of any tag. :only-of-type allows siblings of other tag names — a lone span among divs can match span:only-of-type but not span:only-child.
Yes. :only-child is standard CSS and works with native querySelectorAll — e.g. document.querySelectorAll("li:only-child"). jQuery has supported it since 1.1.4.

Next: :nth-child() Selector

Select children by formula — odd, even, or an+b positions.

:nth-child() 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.

11 people found this page helpful