jQuery :parent Selector

Beginner
5 min read
Updated: Nov 2026
3 examples
Selectors

Definition

:parent selects every element that has at least one child node — an element or a text node. It is the inverse of :empty. Available since jQuery 1.0.

Syntax

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

jQuery
$(":parent")
$("td:parent")              → cells with text or elements
$(".cell").filter(":parent") → official performance pattern

<td>Value 1</td>  → matches (:parent)
<td></td>         → no match (empty)
<div> </div>      → matches (whitespace text counts)

≠ $("span").parent()  // method — climbs UP
CallWhat it does
Base selectorLimits which elements are tested
:parentKeeps elements with ≥1 child node
Child nodesElements or text (including whitespace)

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

Parameters

ParameterDescription
Step 1Choose a base selector (td, .box, and so on).
Step 2Add :parent, or use .filter(":parent") after a CSS query.
Step 3Remember: text nodes count — even a space.

Return Value

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

jQuery Examples

Example 1 — Official Demo: td:parent

Find table cells that contain text or elements (jQuery API example).

jQuery
var $hits = $("td:parent");
console.log($hits.length);
console.log($hits.map(function () {
  return this.textContent.trim() || "(empty text)";
}).get().join(", "));
Try It Yourself

How It Works

  1. Select all cells. Four td elements exist in the demo table.
  2. Keep has-children. :parent keeps cells with a text child.
  3. Drop empties. Truly empty cells have zero child nodes.

Example 2 — :parent vs :empty

Every in-scope element is one or the other — they are inverses.

jQuery
console.log(":parent →", $(".box:parent").length);
console.log(":empty →", $(".box:empty").length);
console.log("total →", $(".box").length);
Try It Yourself

How It Works

  1. Text box. Matches :parent.
  2. Span box. Matches :parent (element child).
  3. Empty box. Matches :empty.

Example 3 — Whitespace Counts as a Child

A single space is a text node — so the element is :parent, not :empty.

jQuery
console.log(":parent →", $("#space:parent").length);
console.log(":empty →", $("#space:empty").length);
console.log("childNodes →", document.getElementById("space").childNodes.length);
Try It Yourself

How It Works

  1. Markup. <div id="space"> </div> holds a space character.
  2. Text node exists. childNodes.length is 1.
  3. :parent matches. Official rule: text nodes count.

Frequently Asked Questions

:parent selects every element that has at least one child node — an element or a text node. $("td:parent") matches table cells that contain text or nested markup. Available since jQuery 1.0.
:empty is the inverse — elements with no children at all. Official docs describe :parent and :empty as opposites. Whitespace-only text makes an element :parent but not :empty.
Yes. Official docs: child nodes include text nodes. A div with only "Hello" (or even a single space) matches div:parent.
:parent filters the matched set for elements that have children. .parent() traverses upward to each element's immediate parent. Official docs point to .parent() / .parents() when you need ancestors.
No. :parent is a jQuery extension and cannot use querySelectorAll alone. Prefer $('.cell').filter(':parent') for performance.
No. Elements empty by definition — input, img, br, hr — never match :parent. A p with only text does match.

Next: :eq() Selector — Pick by Zero-Based Index

Select a single element by its zero-based index in the matched set.

:eq() Selector — Pick by Zero-Based Index 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