jQuery .empty(): Clear Content and Keep the Container
jQuery .empty() removes all child nodes from each matched element. The container and its attributes stay in place, so you can refill a list or results area after clearing its elements and text.
Clear child nodes and keep the container
The jQuery .empty() method takes no arguments. It removes direct text and nested elements and returns the original jQuery collection. With the full example below freshly opened, run this snippet in the browser console.
const $box = $("#container");
const same = $box.empty() === $box;
console.log(same, $box[0].childNodes.length); // true, 0
The console logs true, 0. If the selection matches several containers, the method empties them all. Here the code selects one #container and counts its actual child nodes.
Run the clear and refill example
Save the code as a UTF-8 file named example.html and open it in a browser. You’ll need internet access to load jQuery 4.0.0. If you ran the console snippet already, refresh before testing the buttons.
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>jQuery .empty() example</title>
<style>
body { font: 1rem/1.6 system-ui, sans-serif; margin: 1rem; }
button, input { font: inherit; box-sizing: border-box; max-width: 100%; }
.marked { border: 2px solid #08716d; padding: .5rem; min-height: 2.5rem; }
output { display: block; overflow-wrap: anywhere; }
</style>
<script src="https://code.jquery.com/jquery-4.0.0.min.js"></script>
</head>
<body>
<h1>jQuery .empty() example</h1>
<p><label for="term">Search term</label> <input id="term" type="text"></p>
<div id="container" class="marked">Existing results<p><button class="pick" type="button">Pick existing result</button></p></div>
<p>
<button id="empty" type="button">Empty content</button>
<button id="fill" type="button">Replace with new result</button>
</p>
<output id="output" aria-live="polite"></output>
<script>
$(function () {
const $box = $("#container");
let picks = 0;
const update = () => {
$("#output").text("Child nodes: " + $box[0].childNodes.length +
" / " + "Number of selections: " + picks);
};
$box.on("click", ".pick", function () {
picks += 1;
update();
});
$("#empty").on("click", function () {
$box.empty();
update();
});
$("#fill").on("click", function () {
const $choice = $("<button>", { type: "button", class: "pick" })
.text("Pick new result");
$box.empty().append($("<p>").append($choice));
update();
});
update();
});
</script>
</body>
</html>
Verify what remains after each action
The results area starts with two child nodes: a text node and a paragraph containing a button. Click Empty content to reduce the count to 0. The border and id="container" remain.
Replace with new result adds one paragraph. Each click runs .empty().append(...), so repeated clicks replace the result without accumulating copies. The example uses .text() to display the button label as plain text.
Track events on the container and its children
Click the new result button and the selection count increases. The surviving container listens with .on("click", ".pick", ...). This is event delegation: the container handles clicks that bubble up from matching descendants. See the jQuery .on() reference for the delegated form.
When jQuery removes descendants, it clears their directly bound jQuery handlers and data stored with .data(). Delegated handlers on the surviving container can handle clicks from newly inserted descendants. Attach any direct handlers to the new children if you need them.
Handle input values and whole containers
Use .remove() to delete the container itself. A text input keeps its current value when you call .empty() because the value is separate from its child nodes. To clear it, pass an empty string to .val(). Try these operations in the example’s console.
const $term = $("#term");
$term.val("Seoul");
$term.empty();
console.log($term.val()); // Seoul
$term.val("");
The console prints Seoul and the final line clears the field. To bring back old child elements with their jQuery state, save them before clearing by using .detach() on those elements. Save any direct text nodes separately if they also need to be restored.




