Example 1 — Empty a Paragraph
Remove all content inside every <p>.
$("p").empty(); How It Works
- Select.
$("p")matches each paragraph. - Clear.
.empty()removes every child node inside. - Text too. Plain text counts as a child — it is deleted.

.empty() removes all child nodes (elements and text) from each matched element since jQuery 1.0. The parent stays in the DOM.
Start with these common calls. Keep them short and readable.
$(".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 | Call | What 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.
| Parameter | Description |
|---|---|
| Step 1 | Select the container element(s) you want to wipe. |
| Step 2 | Call .empty() — no arguments. |
| Step 3 | Children (and nested descendants) are removed; the parent remains. |
| Return Value | Description |
|---|---|
| jQuery object | Most setters and traversing helpers return the jQuery collection so you can chain more methods. |
Remove all content inside every <p>.
$("p").empty(); $("p") matches each paragraph..empty() removes every child node inside..empty() vs .remove()Clear inside a box, or delete the box entirely.
$("#a").empty(); // <div id="a"></div> stays
$("#b").remove(); // #b is gone from the DOM #a; the div remains selectable.#b from the document tree.Common pattern: clear a list, then add fresh items.
var fruits = ["Apple", "Banana", "Cherry"];
var $list = $("#fruits").empty();
fruits.forEach(function (name) {
$list.append($("<li>").text(name));
}); .empty() returns the same #fruits collection.<li> elements are created with .text().Delete matched elements from the document and clean up their data.
CODETOFUN NEWS
Get the latest IT news, programming updates, and technology trends directly on your phone.
Your daily dose of tech, straight to your feed.
8 people found this page helpful