jQuery :empty Selector

Beginner
7 min read
Updated: Oct 2026
5 examples
Live preview

What Is :empty?

:empty (since jQuery 1.0) selects elements that have no children at all — no element nodes and no text nodes. It is the inverse of :parent and is a standard CSS pseudo-class.

Remember
$("td:empty")      // empty table cells
$("div:empty")     // empty divs (no text, no kids)
$(":empty")        // avoid — scans every element

// <div></div>     → :empty ✓
// <div> </div>    → NOT empty (whitespace text node)
// :parent          → inverse of :empty
NeedUse
No children at all$("td:empty")
Has any child / text$(":parent")
Has a matching child$(":has(sel)")
Contains a text string$(":contains('x')")
Clear contents.empty()

How to Use :empty

Prefix with a tag, then decide what to do with truly empty nodes.

  1. Prefer $("td:empty") or $("div:empty") over bare $(":empty").
  2. Remember whitespace and newlines between tags create text nodes.
  3. Void elements (img, input, br, …) always match :empty.
  4. Use :parent when you want the opposite set.
  5. To clear content in code, call .empty() — that is a method, not this selector.
PieceRequired?Role
:emptyYesMatches nodes with zero children
Tag prefixRecommendedAvoids a universal * scan
Text nodes—Count as children (including spaces)
Return value—jQuery collection of matching elements

Pseudocode

Pseudocode
matches = []
for each element in search context:
    if element.childNodes.length === 0:
        matches.add(element)

return matches

// Note: a single whitespace text node → length 1 → NOT empty

Cheat Sheet

Keep these facts handy when checking for empty nodes.

TopicDetail
Syntax$("td:empty")
SincejQuery 1.0 · CSS standard
ChildrenElements and text nodes
Inverse:parent
WhitespaceMakes an element non-empty
Void tagsimg, input, br, … always empty
Bare formAvoid $(":empty")
Method twin.empty() clears contents

Live Preview

Run td:empty on a mini table like the official demo.

Sample table

TD #0
TD #2TD#5

Official idea: find cells with no children, then write text into them.

Result
$("td:empty").length
→ 3

empty cells → 3

jQuery Examples

Short, copy-ready snippets. Open DevTools Console, or use Try It Yourself for a live editor.

Example 1 — Official $("td:empty")

Find empty cells and write a placeholder into them.

jQuery
$("td:empty").text("Was empty!");

$("#out").text(
  "Filled cells: " + $("td:contains('Was empty!')").length
);
Try It Yourself

How It Works

  1. Select. $("td:empty") finds cells with no children.
  2. Write. .text("Was empty!") fills only that set.
  3. Skip. Cells that already had text stay unchanged.
  4. Same as docs. Matches the official jQuery demo idea.

Example 2 — :empty vs :parent

Official pair: :empty is the inverse of :parent.

jQuery
$("#out").text(
  "td:empty → " + $("td:empty").length + "\n" +
  "td:parent → " + $("td:parent").length
);
Try It Yourself

How It Works

  1. Empty. Cells with zero child nodes.
  2. Parent. Cells that contain text or elements.
  3. Split. On a six-cell table, counts often add up.
  4. Rule. Pick :empty to find blanks; :parent for filled ones.

Example 3 — Whitespace Is Not Empty

A space or newline inside a tag creates a text node.

jQuery
$("#out").text(
  "Truly empty: " + $("#a:empty").length + "\n" +
  "Has space: " + $("#b:empty").length + "\n" +
  "#b is :parent? " + $("#b").is(":parent")
);
Try It Yourself

How It Works

  1. #a. <div id="a"></div> has no child nodes → empty.
  2. #b. A space between tags is a text node → not empty.
  3. Parent. That space makes #b match :parent.
  4. Fix. Trim markup or use .text().trim() === "" if you need “visually blank.”

Example 4 — Find Empty Containers

Label empty divs so you can spot unfinished UI slots.

jQuery
$("#panel div:empty").attr("data-empty", "yes");

$("#out").text(
  "Empty divs: " + $("#panel div:empty").length
);
Try It Yourself

How It Works

  1. Scope. #panel div:empty only searches that panel.
  2. Mark. We set a data attribute on each empty div.
  3. Skip. Divs with text or child elements do not match.
  4. Use. Useful for placeholders and incomplete widgets.

Example 5 — Void Elements Are Always Empty

Tags like input and img never have children.

jQuery
$("#out").text(
  "input:empty → " + $("input:empty").length + "\n" +
  "img:empty → " + $("img:empty").length + "\n" +
  "div:empty → " + $("div:empty").length
);
Try It Yourself

How It Works

  1. Void. input / img cannot contain children.
  2. Always. They therefore match :empty.
  3. Containers. Only truly blank divs join that set.
  4. Tip. Scope carefully if you do not want void tags in the result.

Edge Cases & Pitfalls

Check these before treating “looks blank” as :empty.

space

Whitespace is a child

Pretty-printed HTML with newlines inside a tag often fails :empty. Keep open/close tags adjacent when you need a true empty node.

bare

Bare $(":empty") is broad

It scans every element type, including void tags. Prefer $("td:empty") or a scoped query.

void

Void elements always match

img, input, br, and hr are empty by definition — exclude them if you only want containers.

method

:empty ≠ .empty()

The selector finds empty nodes. The method .empty() removes children from a matched set.

Browser Support

The :empty pseudo-class is standard CSS and has been in jQuery since 1.0. It works wherever your jQuery build runs.

✓ Stable · CSS · jQuery 1.0+

jQuery :empty

Supported across jQuery 1.x, 2.x, and 3.x in every browser that runs your jQuery build. Prefer current jQuery 3.x for modern projects.

100% Pseudo-class support
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: Safe everywhere jQuery runs. Prefer a tag prefix, and remember whitespace text nodes prevent a match.

Performance Notes

ApproachCostBest for
$("td:empty")CheapTables and lists
$("#panel div:empty")Cheapest scopedLarge pages
$(":empty")Slower / noisyAvoid — universal + void tags
.filter(fn) on text trimModerate“Visually blank” checks

Prefix and scope keep results meaningful. Bare :empty often returns more void elements than you expect.

Key Takeaways

  • Role: match elements with zero child nodes.
  • Text counts: whitespace makes a node non-empty.
  • Inverse: use :parent for elements with children.
  • Clear: remove children with .empty().

One line: :empty finds nodes with no children — including no text — so keep tags tight and prefix with a tag name.

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. 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. Prefer $("td:empty"), $("div:empty"), or $("#panel li:empty").
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.
Void elements like img, input, br, and hr have no children by definition, so they always match :empty.
Yes. :empty is a standard CSS pseudo-class. jQuery has supported it since 1.0. Prefer a tag prefix for clarity and performance.

Next: :parent Selector

Learn the inverse of :empty — elements that have at least one child.

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

8 people found this page helpful