.detach() removes matched elements from the DOM without wiping jQuery data or event handlers (since 1.4). Same removal as .remove(), but safe to reinsert later.
Syntax
Start with these common calls. Keep them short and readable.
jQuery
var stash = $("p").detach(); // off the page, data kept
stash.appendTo("body"); // back on — handlers still work
$(".item").detach(".gone") // only items matching .gone
$("p").remove() // also clears jQuery data/events
Call
What it does
Matched set
Elements to remove from the document
selector
Further filter inside the matched set
Return
Same collection — data and events kept
Load jQuery before your script (CDN or local file), then wrap DOM work in $(function () { ... }) when needed.
Parameters
Parameter
Description
Step 1
Select the element(s) to take off the page.
Step 2
Call .detach() (optionally with a filter selector).
Step 3
Store the returned jQuery collection in a variable.
Return Value
Return Value
Description
See examples
Check the examples below for what this API returns in common cases.
jQuery Examples
Example 1 — Detach and Store
Take paragraphs off the page and keep the collection.
jQuery
var p = $("p").detach();
console.log($("p").length); // 0
console.log(p.length); // still 3 (held in memory)
Paragraphs return inside #stage
Prior .on() handlers still fire
How It Works
Park..detach() returns the live collection.
Wait. Do other UI work while nodes are off-DOM.
Restore..appendTo("#stage") inserts them as last children.
Example 3 — .detach() vs .remove()
Same visual removal — different fate for data and events.
jQuery
$("#a").data("n", 1).on("click", handler);
$("#b").data("n", 1).on("click", handler);
var kept = $("#a").detach(); // data + handler kept
$("#b").remove(); // data + handler cleared
kept.appendTo("#stage"); // click still works
// $("#b") is gone for good
detach → can reinsert with data
remove → cannot recover handlers
How It Works
Same start. Both elements have data and a click handler.
detach. Node leaves the DOM; jQuery cache stays.
remove. Node leaves and jQuery cleans associations.
Frequently Asked Questions
.detach() removes matched elements from the DOM and returns the same jQuery collection. Unlike .remove(), it keeps jQuery data and event handlers so you can reinsert the nodes later. Available since jQuery 1.4.
Both take elements out of the document. .detach() preserves .data() and handlers. .remove() also cleans up jQuery associations — data and handlers are lost. Use detach for temporary removal; use remove when nodes are gone for good.
Yes. Handlers bound with .on() stay on the detached collection. After stash.appendTo(target), they fire again — that is the official demo pattern.
Yes. Pass a selector: .detach(".inactive") removes only matches that also pass the filter. With no argument, every element in the set is detached.
Store the return value: var stash = $("#panel").detach(); later stash.appendTo("#container") or stash.prependTo("body").
.detach() removes the matched elements themselves. .empty() removes children inside the matched element and leaves the parent in place.