jQuery .empty() Method

Beginner
5 min read
Updated: Nov 2026
3 examples
DOM

Definition

.empty() removes all child nodes (elements and text) from each matched element since jQuery 1.0. The parent stays in the DOM.

Syntax

Start with these common calls. Keep them short and readable.

jQuery
$(".hello").empty()
// <p class="hello">Hello</p>  →  <p class="hello"></p>

$("#list").empty().append("<li>New</li>")  // clear, then rebuild

$("p").remove()   // deletes the <p> itself
$("p").empty()    // keeps <p>, clears inside
CallWhat it does
.empty()Learn jQuery .empty(): clear all child nodes while keeping the parent, empty vs remove vs detach vs html, memory cleanup, examples, pitfalls, and FAQs.

Load jQuery before your script (CDN or local file), then wrap DOM work in $(function () { ... }) when needed.

Parameters

ParameterDescription
Step 1Select the container element(s) you want to wipe.
Step 2Call .empty() — no arguments.
Step 3Children (and nested descendants) are removed; the parent remains.

Return Value

Return ValueDescription
jQuery objectMost setters and traversing helpers return the jQuery collection so you can chain more methods.

jQuery Examples

Example 1 — Empty a Paragraph

Remove all content inside every <p>.

jQuery
$("p").empty();
Try It Yourself

How It Works

  1. Select. $("p") matches each paragraph.
  2. Clear. .empty() removes every child node inside.
  3. Text too. Plain text counts as a child — it is deleted.

Example 2 — .empty() vs .remove()

Clear inside a box, or delete the box entirely.

jQuery
$("#a").empty();   // <div id="a"></div> stays
$("#b").remove();  // #b is gone from the DOM
Try It Yourself

How It Works

  1. empty. Wipes children of #a; the div remains selectable.
  2. remove. Deletes #b from the document tree.
  3. Reuse. Emptied containers can be filled again.

Example 3 — Empty Then Append

Common pattern: clear a list, then add fresh items.

jQuery
var fruits = ["Apple", "Banana", "Cherry"];
var $list = $("#fruits").empty();
fruits.forEach(function (name) {
  $list.append($("<li>").text(name));
});
Try It Yourself

How It Works

  1. Empty. Old list items are removed safely.
  2. Chain. .empty() returns the same #fruits collection.
  3. Append. New <li> elements are created with .text().

Frequently Asked Questions

.empty() removes all child nodes from each matched element — text and nested elements — while the parent stays in the DOM. Returns the same jQuery collection for chaining. Available since jQuery 1.0.
.empty() clears inside the matched element and leaves the parent. .remove() deletes the matched elements themselves. Use empty to wipe a container; use remove to delete the container too.
.empty() destroys children and cleans their jQuery data/handlers. .detach() removes matched elements but keeps data and handlers for later reinsertion. Use empty to clear contents; use detach to stash whole nodes.
Yes. In the DOM, text inside an element is a child node. .empty() removes text as well as element children.
No. To remove only some children, select those descendants and call .remove() or .detach() on them.
Yes. jQuery removes data and handlers from child elements before removing them, which helps avoid memory leaks.
Both can clear contents. .empty() is the dedicated clearer and always cleans child data/handlers. Prefer .empty() when your intent is “clear this container.”

Next: .html() Method

Delete matched elements from the document and clean up their data.

.html() Method tutorial →

CODETOFUN NEWS

Stay Ahead in Tech

Get the latest IT news, programming updates, and technology trends directly on your phone.

Your daily dose of tech, straight to your feed.

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