.detach( [ selector ] ) Returns: jQuery

Description: Remove the set of matched elements from the DOM.

  • version added: 1.4.detach( [ 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

<!DOCTYPE html>
  <style>p { background:yellow; margin:6px 0; } p.off { background: black; }</style>
  <script src="http://code.jquery.com/jquery-latest.js"></script>
  how are 
  <button>Attach/detach paragraphs</button>
    var p;
      if ( p ) {
        p = null;
      } else {
        p = $("p").detach();