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
Fundamentals
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").
Concept
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.
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)" );
Cheat Sheet
⚡ Quick Reference
Markup
:empty
:parent
<td></td>
Matches
No match
<td>Data</td>
No match
Matches
<div> </div> (space inside)
No match
Matches
<div><span></span></div>
No match
Matches
<input> (void element)
Matches
No match
Compare
📋 :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
Hands-On
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
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
2 empty li removed
List shows "Buy milk" and "Walk dog" only
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.
Applications
🚀 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.
Important
📝 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.
Compatibility
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 ChromeAll versions · Desktop & Mobile
Full support
Mozilla FirefoxAll versions · Desktop & Mobile
Full support
Apple SafariAll versions · macOS & iOS
Full support
Microsoft EdgeAll versions · Chromium & Legacy
Full support
Internet ExplorerIE 6+ · Legacy environments
Full support
OperaAll modern versions
Full support
:emptyUniversal
Bottom line: Use td:empty or div:empty for scoped queries. Remember whitespace text nodes prevent a match.
Wrap Up
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.
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
Summary
Key Takeaways
Knowledge Unlocked
Five things to remember about :empty
No child nodes — elements or text.
5
Core concepts
∅01
:empty
No children
API
td02
Prefix
td:empty
Scope
par03
Inverse
:parent
Compare
sp04
Whitespace
Text counts
Tip
demo05
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.