.detach( [selector ] )Returns: jQuery

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

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>
<html lang="en">
  <meta charset="utf-8">
  <title>detach demo</title>
  p {
    background: yellow;
    margin: 6px 0;
  p.off {
    background: black;
  <script src="https://code.jquery.com/jquery-3.5.0.js"></script>
how are
<button>Attach/detach paragraphs</button>
$( "p" ).click(function() {
  $( this ).toggleClass( "off" );
var p;
$( "button" ).click(function() {
  if ( p ) {
    p.appendTo( "body" );
    p = null;
  } else {
    p = $( "p" ).detach();


© The jQuery Foundation and other contributors
Licensed under the MIT License.