The .empty() method removes every child node inside matched elements — text, elements, and nested descendants — while the parent stays in the DOM. This tutorial covers syntax since 1.0, comparisons with .detach(), .remove(), and .html(), the official empty demo, and five practical try-it examples.
01
.empty()
Clear children
02
Parent
Stays put
03
Text nodes
Removed too
04
Cleanup
Data & events
05
vs .detach()
Preserve state
06
Since 1.0
Core API
Fundamentals
Introduction
Dynamic UIs constantly refresh content — clear a message list before fetching new notifications, wipe a table body before rendering updated rows, or reset a widget panel without destroying its outer shell. When you need to remove everything inside an element but keep the container itself, jQuery provides .empty().
Available since jQuery 1.0, .empty() removes all child nodes from each matched element. That includes text nodes (plain strings of text between tags) and every nested descendant element. The matched parent remains in the document tree, ready to receive new content via .append(), .html(), or .load().
Unlike .detach(), which preserves jQuery data and event handlers on removed nodes, .empty() actively cleans up child associations to prevent memory leaks. Use .detach() when you plan to reinsert nodes; use .empty() when child content should be discarded. Browse the jQuery DOM hub for related manipulation methods.
Concept
Understanding the .empty() Method
.empty() takes no arguments. For each element in the jQuery collection, it removes every child node — text, comments, and elements at any depth — then returns the same jQuery collection for chaining.
The parent element itself is untouched: its tag, attributes, and jQuery data remain. Only descendants are removed. Before deleting child elements, jQuery strips their event handlers and internal data cache entries so orphaned listeners do not leak memory.
💡
Beginner Tip
Think of .empty() as “clear the box but keep the box.” The official jQuery demo calls $( "p" ).empty() on button click — every paragraph becomes an empty <p></p> tag with no text inside.
Foundation
📝 Syntax
jQuery .empty() has a single form — no parameters:
Remove all child nodes — since 1.0
jQuery
.empty()
Removes all child nodes (text and elements) from each matched element.
Returns the jQuery object for chaining.
Does not accept a selector or filter argument.
Return value
Always the same jQuery collection that was emptied.
Chain .append(), .html(), or .load() immediately after.
Official empty pattern
The jQuery API documentation demo clears all paragraph content on button click:
Before: <p>Hello</p>. After: <p></p> — the paragraph tag remains, text is gone.
Cheat Sheet
⚡ Quick Reference
Goal
Code
Clear all children inside a container
$("#container").empty()
Empty then append fresh content
$("#list").empty().append("<li>New</li>")
Clear table body before new rows
$("#results tbody").empty()
Remove matched elements entirely
$("#temp").remove()
Stash nodes with events intact
var stash = $("#panel").detach()
Replace inner HTML as string
$("#box").html("<p>Fresh</p>")
Compare
📋 .empty() vs .detach() vs .remove() vs .html()
Four related DOM APIs — pick the right clearing or removal strategy.
.empty()
children
Remove all child nodes inside matched elements; parent stays in DOM
.detach()
keep data
Remove matched elements from DOM; preserve jQuery data and events for reinsertion
.remove()
discard
Remove matched elements and clean up jQuery associations permanently
.html()
replace
Set inner HTML as a string — also clears existing children when assigning new markup
Hands-On
Examples Gallery
Examples 1–5 follow the official jQuery API documentation and common production patterns. Use DevTools or the Try-it links to run each snippet in the browser.
📚 Getting Started
Official jQuery demo and container-clearing patterns.
Example 1 — Official Demo: Empty All Paragraphs
Button click removes all text and child content from every paragraph. The empty <p> tags remain in the DOM.
Before: <p>Hello, Person and person.</p>
After click: <p></p> (empty tags remain)
All paragraph text removed — parent elements stay in DOM
How It Works
.empty() iterates matched elements and removes every child node. Text nodes count as children in the DOM, so inline text disappears along with any nested markup inside the paragraphs.
Example 2 — Clear Container Before Loading New Content
Wipe a results panel before injecting fresh HTML from an Ajax response.
jQuery
$( "#refresh" ).on( "click", function() {
var $panel = $( "#results" );
$panel.empty().append( "<p class='loading'>Loading…</p>" );
$.get( "/api/items", function( html ) {
$panel.empty().append( html );
});
});
Click refresh → old results removed
Loading placeholder shown
Ajax completes → panel emptied again, new HTML appended
#results div itself never leaves the DOM
How It Works
.empty() guarantees no stale nodes linger when new content arrives. Chaining .empty().append() is a common pattern for single-container UIs that swap content on user action or server updates.
📈 Practical Patterns
Parent preservation, nested cleanup, and list refresh workflows.
Example 3 — Parent Element Stays in the DOM
Contrast what .empty() removes versus what it keeps.
Text "Hello" removed
Nested <span> removed
#box div still exists with id and class intact
Use .remove() instead to delete #box entirely
How It Works
.empty() only affects descendants. The matched element’s opening tag, attributes, and any jQuery data bound to the parent survive. This makes it ideal for reusable container shells in component-style markup.
Example 4 — All Nested Descendants Removed
Deeply nested markup inside a parent is fully cleared in one call.
jQuery
$( "#sidebar" ).empty();
// Removes every descendant at any depth:
// <ul>, <li>, <a>, text nodes — all gone
// <aside id="sidebar"></aside> remains
Multi-level nav tree inside #sidebar → fully cleared
No need to loop or call .remove() on each child
jQuery cleans up child event handlers during removal
How It Works
You do not need to traverse or manually remove each nested node. A single .empty() call clears the entire subtree under the matched element. jQuery removes child data and handlers before discarding nodes to prevent memory leaks.
Example 5 — Refresh a List From New Data
Empty a list, then rebuild items from a JavaScript array — common CRUD UI pattern.
Old <li> elements removed from #item-list
Two new items appended with .data("id", …)
Re-render on filter/sort without duplicating rows
How It Works
Calling .empty() at the start of a render function ensures idempotent updates — running render twice does not stack duplicate rows. Old child handlers are cleaned up automatically before new nodes are created.
Applications
🚀 Common Use Cases
Ajax panels — clear a container before injecting server HTML or JSON-rendered markup.
Table bodies — $("#results tbody").empty() before rendering paginated rows.
Dynamic lists — rebuild todo, cart, or notification lists from fresh data arrays.
Form reset areas — wipe dynamically added form fields inside a wrapper without removing the wrapper.
Widget shells — keep a styled container div and swap inner content on theme or layout changes.
Error cleanup — remove stale validation messages inside a form section before re-validating.
🧠 How .empty() Clears Child Nodes
1
Match parent elements
jQuery collection points at container nodes that stay in the document.
Select
2
Walk child nodes
Every child — text, comments, and nested elements — is targeted for removal.
Descendants
3
Clean up jQuery state
Event handlers and .data() on child elements are removed to prevent leaks.
Cleanup
4
🗑
Return empty parent
Same jQuery collection with hollow containers — ready for new content.
Important
📝 Notes
Available since jQuery 1.0 — one of the earliest DOM manipulation methods.
Takes no arguments; there is no selector filter like .detach( selector ).
Removes text nodes as well as element children — inline text counts as a child node.
Parent attributes, id, class, and jQuery data on the matched element are preserved.
jQuery cleans up child event handlers and data before removal to avoid memory leaks.
Use .detach() on children if you need to preserve their jQuery state for reinsertion.
.html("") also clears children but parses HTML strings; .empty() is clearer when you only need to wipe content.
Compatibility
Browser Support
.empty() has been part of jQuery since 1.0+. It relies on standard DOM child-node removal under the hood. Works in all browsers supported by your jQuery build — IE6+ through modern evergreen browsers in legacy jQuery versions, and all current browsers in jQuery 3.x and 4.x.
✓ jQuery 1.0+
jQuery .empty()
Universal DOM clearing API across jQuery 1.x, 2.x, 3.x, and 4.x. No browser-specific polyfills required — jQuery normalizes child-node removal. Pair with .append() or .load() to refill containers.
100%With jQuery loaded
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
.empty()Universal
Bottom line: Default choice for clearing container contents while keeping the parent element.
Wrap Up
Conclusion
The jQuery .empty() method removes all child nodes from matched elements while keeping the parent in the DOM. It clears text, nested markup, and associated jQuery state on children — exactly as the official demo demonstrates when paragraphs are wiped with a button click.
Choose .detach() when you need to stash nodes with events intact. Next up: permanently deleting matched elements with .remove().
Use .empty() before re-rendering dynamic lists or panels
Chain .empty().append() for atomic container updates
Keep reusable container shells and swap inner content with empty
Prefer .empty() over manual child loops for clarity
Use .detach() instead when child nodes must survive removal
❌ Don’t
Use .empty() when you need to delete the parent element itself
Expect removed child event handlers to fire after .empty()
Confuse .empty() with .detach() (children vs self)
Append new content without emptying first — duplicates stack up
Pass a selector to .empty() — it accepts no arguments
Summary
Key Takeaways
Knowledge Unlocked
Five things to remember about .empty()
The clear-the-box API — parent stays, children go.
5
Core concepts
∅01
Children
All removed
Clear
□02
Parent
Stays put
Shell
T03
Text
Nodes too
DOM
→04
Chain
.append()
Refill
≠05
vs detach
Preserve
Compare
❓ Frequently Asked Questions
.empty() removes all child nodes from each matched element — including text nodes and nested elements — while the matched parent stays in the DOM. It returns the same jQuery collection for chaining. Available since jQuery 1.0.
.empty() clears everything inside the matched element but leaves the parent node in place. .remove() deletes the matched elements themselves from the document. Use empty to wipe a container; use remove to delete the container too.
.empty() removes child nodes and jQuery cleans up their data and event handlers to prevent memory leaks. .detach() removes matched elements from the DOM but preserves jQuery data and handlers so you can reinsert them later. Use empty to clear contents; use detach to stash whole nodes.
Yes. According to the DOM specification, text inside an element is a child node. .empty() removes text as well as element children — for example, $(".hello").empty() removes the word Hello but keeps the empty paragraph tag.
No. .empty() takes no parameters. To remove only some children, use .remove() with a selector on descendants, or .detach() if you need to preserve jQuery state on removed nodes.
Yes. Before removing child elements, jQuery removes associated data and event handlers to avoid memory leaks. If you need to preserve handlers for later reinsertion, detach the children instead of emptying the parent.
Did you know?
Before jQuery, clearing a container meant looping over element.childNodes and calling removeChild on each node — easy to miss text nodes and leak event listeners. .empty() has been in jQuery since version 1.0 and handles text, elements, and jQuery cleanup in one call. It is also safer than assigning innerHTML = "" in older IE, where that pattern could leave orphaned references in certain edge cases.