jQuery .remove(): Remove Elements, Data, and Events
jQuery .remove() deletes selected elements from the Document Object Model (DOM), together with the text and descendant nodes they contain. An optional selector limits removal to matches within the current selection. If you plan to reuse a removed node, check whether its jQuery data and event handlers need to survive.
Choose which matched elements to remove
$(".item").remove() removes every selected .item. Passing ".obsolete" limits removal to selected elements with that class. The official jQuery .remove() reference describes the selector argument and return value.
To stay within the active list, select its items first with $("#container .item") or $("#container").find(".item"). Calling $("#container").remove(".obsolete") tests the container itself against the selector.
const $items = $("#container .item");
const $result = $items.remove(".obsolete");
console.log($result === $items, $("#container .item").length); // true, 1
With the full example below freshly loaded, run the snippet above in the browser console. It logs true, 1. The return value is the original jQuery collection, which can still reference the removed node. Query the DOM again to count the remaining items.
Run an example scoped to one of two lists
Save this as a UTF-8 file named example.html and open it in a browser. An internet connection is needed to load jQuery 4.0.0. Refresh the page before testing the button if you already ran the console snippet.
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>jQuery .remove() example</title>
<style>
body { font: 1rem/1.6 system-ui, sans-serif; margin: 1rem; }
button { font: inherit; max-width: 100%; }
output { display: block; overflow-wrap: anywhere; }
</style>
<script src="https://code.jquery.com/jquery-4.0.0.min.js"></script>
</head>
<body>
<h1>jQuery .remove() example</h1>
<h2>Active list</h2>
<ul id="container">
<li id="old" class="item obsolete"><span>Obsolete item</span></li>
<li id="keep" class="item">Item to keep</li>
</ul>
<button id="remove" type="button">Remove obsolete items from the active list</button>
<output id="output" aria-live="polite"></output>
<h2>Archive</h2>
<ul id="archive">
<li class="item obsolete">Archived obsolete item</li>
</ul>
<script>
$(function () {
const $list = $("#container");
const update = () => {
$("#output").text("Items remaining in active list: " + $list.find(".item").length);
};
$("#remove").on("click", function () {
$list.find(".item").remove(".obsolete");
update();
});
update();
});
</script>
</body>
</html>
Check what was removed
The active list starts with two items. Click Remove obsolete items from the active list. The obsolete item and its nested span disappear, and the count drops to 1. Clicking again leaves the list as it is.
The archive item also has the .obsolete class. It stays because it is outside the selected list. In the browser’s element inspector, check that #old is absent and that #container still contains #keep.
Compare data and events after reinsertion
.remove() clears jQuery data added with .data() and event handlers attached with .on() on the removed elements and their descendants. .detach() preserves them. On the example page, run this block in the browser console to reinsert and click a test button after each kind of removal.
for (const method of ["remove", "detach"]) {
const $test = $("<button type='button'></button>")
.text("Test button").insertAfter("#container");
let clicks = 0;
$test.data("demoState", "saved");
$test.on("click", () => { clicks += 1; });
$test[method]().insertAfter("#container");
$test.trigger("click");
console.log(method, $test.data("demoState"), clicks);
$test.remove();
}
The console shows remove undefined 0 and then detach saved 1. Each button is reinserted. Only the detached button keeps the original data and click handler.
Choose a method for clearing or hiding content
To retain the list element and clear its text and child nodes, call .empty(), for example $("#container").empty(). To delete just an HTML attribute such as title, use .removeAttr().
For temporarily hiding content and showing it in the same place later, use .hide() or a CSS class. The hidden node is still in the DOM and can be shown again.
For an undo action, save the original order or insertion point. Use .detach() to restore the same node with its jQuery state. If you render new nodes from data, initialize any needed data and direct event handlers on the new elements.




