Remove the set of matched elements from the DOM.

.detach(selector)🡢 jQuery

selector SelectorA selector expression that filters the set of matched elements to be removed.

The .detach() method is the same as .remove(), except that .detach() keeps all jQuery data associated with the removed elements. This method is useful when removed elements are to be reinserted into the DOM at a later time.

Detach all paragraphs from the DOM

how are
<button>Attach/detach paragraphs</button>
p {
  background: yellow;
  margin: 6px 0;
p.off {
  background: black;
$("p").click(function () {
var p;
$("button").click(function () {
  if (p) {
    p = null;
  } else {
    p = $("p").detach();

Looking for a Web Developer?


Hi! I'm Basti, author of this site. If you are looking for a web developer with 15+ years of experience, holla at me!

Be it the good 'ol jQuery, vanilla JS or modern frameworks like Vue and Svelte, front- or backend, I can help you.

Just write me at jobs@jqapi.com :)