jQuery .detach() Method

Beginner
6 min read
Updated: Oct 2026
4 examples
Live preview

What Is .detach()?

.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
NeedUse
Park nodes for latervar s = $(el).detach()
Put them backs.appendTo(target)
Delete for good$(el).remove()
Clear children only$(el).empty()

How to Use .detach()

Remove, hold the return value, then reinsert when needed.

  1. Select the element(s) to take off the page.
  2. Call .detach() (optionally with a filter selector).
  3. Store the returned jQuery collection in a variable.
  4. Later call .appendTo() / .prependTo() on that stash.
PieceRequired?Role
Matched setYesElements to remove from the document
selectorOptionalFurther 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

Cheat Sheet

Keep these facts handy while parking DOM nodes.

TopicDetail
Syntax.detach([selector])
SincejQuery 1.4
vs removeKeeps data + events
vs emptyRemoves the elements, not only children
Reinsertstash.appendTo(target)
Filter.detach(".temp") — subset only
ReturnDetached jQuery collection

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.

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)
Try It Yourself

How It Works

  1. Select. $("p") matches the paragraphs.
  2. Detach. They leave the document tree.
  3. Store. Variable p still references those nodes.
  4. DOM query. A fresh $("p") finds nothing until you reinsert.

Example 2 — Reinsert With .appendTo()

Put the stash back — events kept from before.

jQuery
var p = $("p").detach();
// … later …
p.appendTo("#stage");
Try It Yourself

How It Works

  1. Park. .detach() returns the live collection.
  2. Wait. Do other UI work while nodes are off-DOM.
  3. Restore. .appendTo("#stage") inserts them as last children.
  4. 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
Try It Yourself

How It Works

  1. Same start. Both elements have data and a click handler.
  2. detach. Node leaves the DOM; jQuery cache stays.
  3. remove. Node leaves and jQuery cleans associations.
  4. Rule. Coming back later? Detach. Gone forever? Remove.

Example 4 — Optional Selector Filter

Detach only the subset that matches an extra selector.

jQuery
$(".item").detach(".temp");
// removes .item.temp only; other .item stay
Try It Yourself

How It Works

  1. Matched set. Starts with every .item.
  2. Filter. ".temp" narrows to items that also have that class.
  3. Detach. Only that subset leaves the document.
  4. 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.

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 Chrome All versions · Desktop & Mobile
Full support
Mozilla Firefox All versions · Desktop & Mobile
Full support
Apple Safari All versions · macOS & iOS
Full support
Microsoft Edge All versions · Chromium & Legacy
Full support
Internet Explorer IE 6+ · Legacy environments
Full support
Opera All modern versions
Full support
.detach() Universal

Bottom line: Safe for temporary UI parking. Reinsert with .appendTo(); use .remove() when the nodes will never return.

Performance Notes

ApproachCostBest for
.detach() + later insertCheap park/restoreToggle panels, tabs, dialogs
.remove() + rebuildRebuild costThrowaway content
.hide() / CSSStill in layout treeSimple 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.

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.

Next: .empty()

Remove all child nodes while keeping the parent element.

.empty() tutorial →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

8 people found this page helpful