jQuery .detach() Method

Beginner
5 min read
Updated: Nov 2026
3 examples
DOM

Definition

.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
CallWhat it does
Matched setElements to remove from the document
selectorFurther filter inside the matched set
ReturnSame collection — data and events kept

Load jQuery before your script (CDN or local file), then wrap DOM work in $(function () { ... }) when needed.

Parameters

ParameterDescription
Step 1Select the element(s) to take off the page.
Step 2Call .detach() (optionally with a filter selector).
Step 3Store the returned jQuery collection in a variable.

Return Value

Return ValueDescription
See examplesCheck 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)
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.

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.

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.

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: .after() Method

Remove all child nodes while keeping the parent element.

.after() Method tutorial →

CODETOFUN NEWS

Stay Ahead in Tech

Get the latest IT news, programming updates, and technology trends directly on your phone.

Your daily dose of tech, straight to your feed.

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