jQuery :parent Selector

Beginner
⏱️ 8 min read
📚 Updated: Jul 2026
🎯 5 Examples
🚀 5 Try-it labs
Content structure

What You’ll Learn

The :parent selector matches elements that have at least one child node — an element or text. Available since jQuery 1.0; it is the inverse of :empty and a jQuery extension (not standard CSS).

01

Syntax

td:parent

02

Has children

Element or text

03

Official

td:parent

04

vs :empty

Inverse filter

05

≠ .parent()

Traversing up

06

.filter()

Performance

Introduction

Tables, lists, and layout containers often mix filled cells with blank ones. jQuery’s :parent pseudo-class keeps elements that contain something inside them — nested markup or even a text node.

Official jQuery documentation describes :parent as the inverse of :empty. Both count text nodes as children, so a paragraph with only whitespace is :parent but not :empty. For climbing the DOM tree to an element’s ancestor, use the .parent() method instead — a different concept entirely.

Understanding the :parent Selector

Think of :parent as “has at least one child node”:

  • td:parent → cells with text like “Value 1” or nested elements.
  • td:empty → truly hollow cells — the complement in the same matched set.
  • <input>, <img>, <br> → empty by HTML definition (no children).
  • <p> with text → matches :parent; W3C recommends at least one child node in paragraphs.
💡
Beginner Tip

:parent does not mean “select my parent element.” It means “this element is a parent — it has children.” To get the parent of a selection, call .parent() on the jQuery object.

📝 Syntax

Official jQuery API form (since 1.0):

jQuery
jQuery( ":parent" )

// typical usage — chain after a selector:
$( "td:parent" )
$( "div.box:parent" )
$( "li:parent" )

// performance pattern:
$( ".cell" ).filter( ":parent" )

// NOT the same as traversing up:
$( "span" ).parent()   // method — returns parent elements

Parameters

  • No arguments — keeps elements with at least one child node (element or text).

Return value

  • A jQuery object containing every matching element (may be multiple).
  • Empty collection when no elements in scope have children.

Official jQuery API example

jQuery
// Finds all tds with children, including text
$( "td:parent" ).fadeTo( 1500, 0.3 );

⚡ Quick Reference

Element state:parent:empty
<td>Value</td>MatchesNo match
<td></td>No matchMatches
<div> </div> (space only)MatchesNo match
<div><span></span></div>MatchesNo match
Native CSSjQuery onlyStandard CSS

📋 :parent vs :empty and .parent()

Has-children filter vs inverse vs upward traversal.

:parent
td:parent

Has child nodes

:empty
td:empty

No child nodes

.parent()
$("span").parent()

Go up one level

.parents()
$("a").parents("ul")

Ancestor chain

Examples Gallery

Example 1 follows the official jQuery td:parent demo. Examples 2–5 compare with :empty, demonstrate the whitespace text trap, use the official .filter(':parent') pattern, and contrast with the .parent() method.

📚 Official jQuery Demo

Fade table cells that contain text or nested elements.

Example 1 — Official Demo: td:parent

Official jQuery demo — cells with “Value 1” and “Value 2” fade to 30% opacity; empty cells stay solid green.

jQuery
$( "td:parent" ).fadeTo( 1500, 0.3 );

// Only tds with child nodes (text counts) are faded
Try It Yourself

How It Works

Text nodes count as children — official docs include “including text” in the demo description.

Example 2 — :parent vs :empty

Split the same set of boxes into filled vs hollow using complementary filters.

jQuery
$( ".box:parent" ).css( "borderColor", "#2563eb" );
$( ".box:empty" ).css( "backgroundColor", "#fee2e2" );

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

How It Works

Official jQuery docs describe :parent as the inverse of :empty — every in-scope element is one or the other.

📈 Practical Patterns

Whitespace trap, performance filtering, and method contrast.

Example 3 — Whitespace Text: div:parent

A div with only a space character matches :parent but fails :empty — same rule as the :empty tutorial.

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

How It Works

Child nodes include text nodes — official docs warn about this for both :parent and :empty.

Example 4 — Performance: .filter(":parent")

Official docs — pure CSS selector first, then jQuery has-children filter.

jQuery
$( ".cell" ).filter( ":parent" ).addClass( "filled" );

// Browser optimizes $(".cell"); jQuery filters to nodes with children
Try It Yourself

How It Works

Because :parent is not standard CSS, splitting the query helps performance on large pages.

Example 5 — Not the Same: :parent vs .parent()

Official docs — use .parent() to climb to ancestors; :parent filters for elements that have children.

jQuery
// :parent — keep li elements that contain something
console.log( "li:parent →", $( "li:parent" ).length );

// .parent() — from the span, go up to its li parent
console.log( "span.parent() →", $( "span" ).parent().prop( "tagName" ) );
Try It Yourself

How It Works

Same word, different direction — selector filters down for has-children; method traverses up the tree.

🚀 Common Use Cases

  • Table cells — highlight td:parent cells that contain data.
  • Inverse of empty — style filled containers while :empty handles hollow ones.
  • List cleanup — skip li:empty by selecting li:parent first.
  • Form validation — detect wrapper divs that received user input (text nodes).
  • Dynamic UI — fade or dim populated slots in dashboards.
  • Performance.filter(':parent') after a CSS class query.

🧠 How jQuery Evaluates :parent

1

Build matched set

Run the selector before :parent — e.g. all td or .box.

Query
2

Inspect child nodes

Check childNodes — elements and text both count.

DOM
3

Keep non-empty

Retain elements with at least one child node — inverse of :empty.

Filter
4

Return collection

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

📝 Notes

  • Available since jQuery 1.0 — inverse of :empty (official docs).
  • Child nodes include text nodes — whitespace prevents :empty and enables :parent.
  • Not standard CSS — jQuery extension only; use .filter(':parent') for performance.
  • Not the same as .parent() or .parents() — those traverse upward.
  • <input>, <img>, <br>, <hr> are empty by definition.
  • W3C recommends <p> have at least one child — text alone satisfies :parent.

Browser Support

The :parent pseudo-class is a jQuery extension — not standard CSS — and works in jQuery 1.0+. It runs in all browsers that support jQuery. Pair with standard CSS :empty for the inverse filter.

jQuery 1.0+ · extension

jQuery :parent Selector

Works in all jQuery versions. Not available in querySelectorAll — use .filter(':parent') after a CSS selector.

100% jQuery only
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
:parent Universal

Bottom line: Learn :parent for has-children filtering; use .parent() when you need to traverse up the DOM.

Conclusion

The :parent selector matches elements with at least one child node — the official td:parent demo fades cells that contain text while empty cells stay solid.

Pair it with :empty, remember text nodes count, and use .parent() when you need ancestors — not a has-children filter.

💡 Best Practices

✅ Do

  • Pair :parent with :empty for inverse queries
  • Use .filter(':parent') after a CSS selector
  • Remember text nodes (including spaces) count
  • Use .parent() when traversing up the tree
  • Scope queries — table td:parent not bare :parent

❌ Don’t

  • Confuse :parent selector with .parent() method
  • Expect :parent in native querySelectorAll
  • Assume visually empty divs match :empty — check whitespace
  • Use :parent to get an element’s parent node
  • Forget void elements are :empty by HTML definition

Key Takeaways

Knowledge Unlocked

Five things to remember about :parent

Has at least one child node.

5
Core concepts
02

:empty

Inverse

Pair
txt 03

Text counts

Nodes rule

Tip
04

.parent()

Traverse up

Compare
demo 05

Official

td fade

Demo

❓ Frequently Asked Questions

:parent selects every element that has at least one child node — either 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 — it selects elements with no children at all (no elements and no text). Official jQuery docs describe :parent and :empty as opposites. Whitespace-only text makes an element :parent but not :empty.
Yes. Official docs emphasize that child nodes include text nodes. A div containing only the text "Hello" matches div:parent. This is the same rule as :empty — even a single space character prevents :empty and enables :parent.
:parent filters the matched set downward — it keeps elements that have children. .parent() traverses upward to each element's immediate parent node. Official docs point to .parent() and .parents() when you need ancestors of an existing selection, not a has-children filter.
No. :parent is a jQuery extension and does not work in native querySelectorAll. :empty is standard CSS; :parent exists only in jQuery selector strings and .filter(':parent').
Official docs: select with a pure CSS selector first, then use .filter(':parent') — e.g. $('.cell').filter(':parent') — so the browser can optimize the initial query before the jQuery extension filter runs.
Did you know?

Official jQuery docs note that <input>, <img>, <br>, and <hr> are empty by definition — they never match :parent. A <p> with only text does match, which aligns with W3C guidance that paragraphs should contain at least one child node.

Continue to :eq() Selector

After content-structure filters, learn how to pick one element by index in a matched set.

:eq() 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