jQuery :empty Selector

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

What You’ll Learn

The :empty selector matches elements that have no children at all — no nested elements and no text nodes. Available since jQuery 1.0; it is the inverse of :parent and is standard CSS.

01

Syntax

td:empty

02

No children

Elements + text

03

Official

td:empty

04

vs :parent

Inverse

05

Whitespace

Text counts

06

Scope

Not bare :empty

Introduction

Empty DOM nodes are common in tables, lists, and layout shells — a table cell with no data, a list item placeholder, or a widget container waiting for AJAX content. jQuery’s :empty pseudo-class finds those elements in one line.

Official jQuery documentation defines :empty as selecting all elements that have no children, including text nodes. That detail matters: a single space between opening and closing tags creates a text child, so the element is not empty. Prefix with a tag or scoped selector — $("td:empty") instead of bare $(":empty"), which implies $("*:empty").

Understanding the :empty Selector

Think of :empty as a structural filter:

  • <td></td> → matches td:empty.
  • <td>Hello</td> → no match (text child).
  • <div><span></span></div> → no match (element child).
  • <div> </div> → no match (whitespace text node).
  • <img>, <input>, <br> — void elements with no children always match.
💡
Beginner Tip

To find elements with content, use :parent instead. To remove or style empty nodes, chain .remove(), .text(), or .css() after :empty.

📝 Syntax

Official jQuery API form (since 1.0):

jQuery
jQuery( ":empty" )



// recommended — prefix with element type:

$( "td:empty" )

$( "div:empty" )

$( "#panel li:empty" )

$( "ul.menu li:empty" )

Parameters

  • No arguments — filters by whether the element has any child nodes.

Return value

  • A jQuery object containing every empty element in the search context.
  • Empty collection when nothing matches.

Official jQuery API example

jQuery
// Finds all td elements that are empty

$( "td:empty" )

  .text( "Was empty!" )

  .css( "background", "rgb(255,220,200)" );

⚡ Quick Reference

Markup:empty:parent
<td></td>MatchesNo match
<td>Data</td>No matchMatches
<div> </div> (space inside)No matchMatches
<div><span></span></div>No matchMatches
<input> (void element)MatchesNo match

📋 :empty vs :parent and :not(:empty)

Structural pseudo-classes — find hollow nodes or filled ones.

:empty
td:empty

No child nodes

:parent
div:parent

Has at least one child

:not(:empty)
li:not(:empty)

Same idea as :parent

.html()
.html("")

Make a node empty

Examples Gallery

Example 1 follows the official jQuery td:empty demo. Examples 2–5 compare with :parent, style empty containers, explain the whitespace trap, and remove empty list items. Use the Try-it links to run each snippet.

📚 Official jQuery Demo

Target empty table cells only — filled cells are skipped.

Example 1 — Official Demo: td:empty

Official jQuery demo — label every empty table cell with text and a background color.

jQuery
$( "td:empty" )

  .text( "Was empty!" )

  .css( "background", "rgb(255,220,200)" );



// Only truly empty cells change — cells with text stay as-is
Try It Yourself

How It Works

jQuery collects only td elements with zero child nodes, then chains .text() and .css() on that subset.

Example 2 — :empty vs :parent

Count inverse matches inside the same container.

jQuery
console.log( ":empty →", $( ".box:empty" ).length );

console.log( ":parent →", $( ".box:parent" ).length );



// Totals should add up to total .box count
Try It Yourself

How It Works

Official jQuery docs describe :parent as the inverse of :empty. Every element is one or the other (unless filtered out of scope).

📈 Practical Patterns

Styling placeholders, whitespace awareness, and DOM cleanup.

Example 3 — Style Empty Containers: div.slot:empty

Show dashed borders on empty layout slots waiting for content.

jQuery
$( "div.slot:empty" ).css({

  border: "2px dashed #94a3b8",

  minHeight: "48px"

});
Try It Yourself

How It Works

Dashboard and CMS UIs often render empty widget shells. :empty highlights them without touching filled panels.

Example 4 — Whitespace Trap: Space Text Nodes

Demonstrate why <div> </div> does not match :empty.

jQuery
console.log( "#a:empty →", $( "#a:empty" ).length ); // 1

console.log( "#b:empty →", $( "#b:empty" ).length ); // 0 — space is a text child



// Trim HTML source or use .html("") before testing :empty
Try It Yourself

How It Works

jQuery follows the CSS definition: child nodes include text. Pretty-printed HTML with indentation often accidentally adds text nodes.

Example 5 — Remove Empty List Items: $("#tasks li:empty").remove()

Clean up blank li nodes after rendering a dynamic list.

jQuery
var n = $( "#tasks li:empty" ).length;

$( "#tasks li:empty" ).remove();

console.log( n + " empty item(s) removed" );
Try It Yourself

How It Works

Select empty nodes, remove them from the DOM, then re-query — the matched set shrinks to zero for :empty on that list.

🚀 Common Use Cases

  • Table formatting — fill or highlight td:empty cells in reports.
  • Widget shells — style div:empty placeholders before AJAX loads content.
  • List cleanup — remove li:empty after templating.
  • Validation — count empty required containers before submit.
  • Inverse queries — pair with :parent to split filled vs hollow nodes.
  • CMS output — hide empty paragraphs or sections in generated HTML.

🧠 How jQuery Evaluates :empty

1

Find candidates

Start from scoped elements — td:empty, not bare :empty.

Scope
2

Inspect child nodes

Check for element children and text nodes — whitespace counts.

DOM
3

Filter empty

Keep only elements with zero child nodes.

Structure
4

Return collection

Matching elements become a jQuery object for chaining.

📝 Notes

  • Available since jQuery 1.0 — standard CSS pseudo-class.
  • Child nodes include text — whitespace between tags prevents a match.
  • Inverse of :parent — official jQuery documentation.
  • Prefer td:empty over bare $(":empty") (implies *:empty).
  • Void elements (img, input, br) are always empty by definition.
  • Re-query after .html() or .text() changes — emptiness can change dynamically.

Browser Support

The :empty pseudo-class is part of standard CSS and works in jQuery 1.0+. Modern browsers support document.querySelectorAll("td:empty"). jQuery evaluates child nodes consistently, including whitespace text nodes.

CSS · jQuery 1.0+

jQuery :empty Selector

Supported in all modern browsers. Native: querySelectorAll("td:empty"). Counts text nodes as children.

100% Standard + jQuery
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
:empty Universal

Bottom line: Use td:empty or div:empty for scoped queries. Remember whitespace text nodes prevent a match.

Conclusion

The :empty selector matches elements with no child nodes — neither nested elements nor text. The official td:empty demo shows how to label hollow table cells in one chained call.

Prefix with an element type, remember the whitespace gotcha, and use :parent when you need the opposite filter.

💡 Best Practices

✅ Do

  • Use td:empty or div:empty
  • Scope to #tableId td:empty
  • Pair with :parent for inverse queries
  • Trim HTML or use .html("") before relying on :empty
  • Re-run selectors after dynamic content loads

❌ Don’t

  • Use bare $(":empty") on large documents
  • Assume pretty-printed HTML with spaces is empty
  • Confuse :empty with zero visible text (hidden nodes still count)
  • Forget void elements always match :empty
  • Expect :empty to match elements with comment-only children in all browsers

Key Takeaways

Knowledge Unlocked

Five things to remember about :empty

No child nodes — elements or text.

5
Core concepts
td 02

Prefix

td:empty

Scope
par 03

Inverse

:parent

Compare
sp 04

Whitespace

Text counts

Tip
demo 05

Official

td demo

Demo

❓ Frequently Asked Questions

:empty matches elements that have no children — no element nodes and no text nodes. $("td:empty") finds table cells with nothing inside them. Available since jQuery 1.0.
:parent is the inverse — it selects elements that have at least one child (element or text). If a div contains only whitespace text, it matches :parent but not :empty.
Bare :empty implies the universal selector — equivalent to $("*:empty"), which scans every element type in the document. Prefer $("td:empty"), $("div:empty"), or $("#panel li:empty") for clarity and performance.
Yes. A text node — even a single space or newline between tags — counts as a child. <div> </div> does not match :empty; <div></div> does. This is a common beginner gotcha.
Void elements like img, input, br, hr, meta, and link have no children by definition, so they always match :empty (unless you add content via invalid markup).
:empty is standard CSS and works in modern querySelectorAll when prefixed with an element type, e.g. document.querySelectorAll("td:empty"). jQuery has supported it since 1.0.
Did you know?

The W3C HTML spec recommends that <p> elements contain at least one child node — even plain text — while void elements like <img> and <input> are empty by definition and always match :empty.

Continue to :parent Selector

Learn the inverse of :empty — match elements that have at least one child node, including text.

:parent 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