.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.
Remember
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
Remove, hold the return value, then reinsert when needed.
Select the element(s) to take off the page.
Call .detach() (optionally with a filter selector).
Store the returned jQuery collection in a variable.
Later call .appendTo() / .prependTo() on that stash.
Piece
Required?
Role
Matched set
Yes
Elements to remove from the document
selector
Optional
Further filter inside the matched set
Return
—
Same collection — data and events kept
Pseudocode
Pseudocode
set = matched elements
if selector given:
set = set.filter(selector)
for each el in set:
remove el from its parent
// keep jQuery .data() and .on() handlers
return set // ready for appendTo / prependTo later
// Contrast:
remove(set) → also wipe jQuery data and handlers
Reference
Cheat Sheet
Keep these facts handy while parking DOM nodes.
Topic
Detail
Syntax
.detach([selector])
Since
jQuery 1.4
vs remove
Keeps data + events
vs empty
Removes the elements, not only children
Reinsert
stash.appendTo(target)
Filter
.detach(".temp") — subset only
Return
Detached jQuery collection
Try it
Live Preview
Toggle paragraphs off and on — handlers stay alive after .detach().
Browser demo of .detach() — click a paragraph after reattach to confirm handlers survive.
Hello
how are
you?
Result
Click Run to detach or reattach paragraphs.
Code
jQuery Examples
Short, copy-ready snippets. Open DevTools Console, or use Try It Yourself for a live editor.
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.
Handlers. Clicks bound earlier still work after reattach.
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
Filter.".temp" narrows to items that also have that class.
Detach. Only that subset leaves the document.
Rest. Non-matching .item elements stay put.
Edge Cases & Pitfalls
Check these before parking large pieces of UI.
hold
Keep a reference
If you drop the return value, the nodes are hard to find again. Always store var stash = $(…).detach().
remove
Do not confuse with .remove()
.remove() looks the same visually but wipes data and handlers — bad for temporary hide/show.
memory
Detached nodes still use memory
Large trees kept in variables forever can leak. Call .remove() when you are truly done.
empty
.empty() is not detach
.empty() clears children and leaves the parent. Use it to wipe contents, not to stash whole widgets.
Compatibility
Browser Support
.detach() has been part of jQuery since 1.4. It works wherever your loaded jQuery version runs.
✓ Stable · jQuery 1.4+
jQuery .detach()
Supported in all browsers that run jQuery 1.4+. Prefer a current jQuery 3.x build for modern projects.
100%jQuery core method
Google ChromeAll versions · Desktop & Mobile
Full support
Mozilla FirefoxAll versions · Desktop & Mobile
Full support
Apple SafariAll versions · macOS & iOS
Full support
Microsoft EdgeAll versions · Chromium & Legacy
Full support
Internet ExplorerIE 6+ · Legacy environments
Full support
OperaAll modern versions
Full support
.detach()Universal
Bottom line: Safe for temporary UI parking. Reinsert with .appendTo(); use .remove() when the nodes will never return.
Analysis
Performance Notes
Approach
Cost
Best for
.detach() + later insert
Cheap park/restore
Toggle panels, tabs, dialogs
.remove() + rebuild
Rebuild cost
Throwaway content
.hide() / CSS
Still in layout tree
Simple visibility only
Detach is ideal when you need nodes completely out of the document but want to avoid rebinding handlers. Do not stash huge trees forever.
Remember
Key Takeaways
Role: remove nodes while keeping jQuery data and events.
Restore:stash.appendTo(target).
vs remove: remove cleans data; detach keeps it.
Store: always keep the returned collection.
One line:.detach() parks elements off-DOM so you can bring them back with handlers intact.
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.