jQuery detach()

Education is not limited to just classrooms. It can be gained anytime, anywhere... - Ravi Ranjan (M.Tech-NIT)

jQuery detach()

The jQuery detach() method is used to remove the selected elements, including all texts and child nodes and keeps only data and events.

This method saves a copy of the removed elements to reinsert them whenever they needed later.

There are some other methods also which are used to remove elements e.g. jQuery remove() method, jQuery empty() method etc. But there is a little difference among them.

jQuery remove() method: This method is used to remove the elements as well as its data and events.

jQuery empty() method: This method is used to remove only the content from the selected elements.

Syntax:

  1. $(selector).detach()  

Example of jQuery detach() method

Let's take an example to demonstrate the effect of jQuery detach() method.

  1. <!DOCTYPE html>  
  2. <html>  
  3. <head>  
  4. <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script>  
  5. <script>  
  6. $(document).ready(function(){  
  7.   $("button").click(function(){  
  8.     $("p").detach();  
  9.   });  
  10. });  
  11. </script>  
  12. </head>  
  13. <body>  
  14. <p>Hello Guys!</p>  
  15. <p>This is javatpoint.com</p>  
  16. <button>Click here to detach all p elements</button>  
  17. </body>  
  18. </html>   

Test it Now

jQuery detach() example 2

  1. <!DOCTYPE html>  
  2. <html lang="en">  
  3. <head>  
  4.   <meta charset="utf-8">  
  5.   <title>detach demo</title>  
  6.   <style>  
  7.   p {  
  8.     background: lightpink;  
  9.     margin: 6px 0;  
  10.   }  
  11.   </style>  
  12.   <script src="https://code.jquery.com/jquery-1.10.2.js"></script>  
  13. </head>  
  14. <body>  
  15. <p>Hello Guys!</p>  
  16. <p>This is javatpoint.com</p>  
  17. <button>Click here to Attach/detach all p elements. </button>  
  18. <script>  
  19. $( "p" ).click(function() {  
  20.   $( this ).toggleClass( "off" );  
  21. });  
  22. var p;  
  23. $( "button" ).click(function() {  
  24.   if ( p ) {  
  25.     p.appendTo( "body" );  
  26.     p = null;  
  27.   } else {  
  28.     p = $( "p" ).detach();  
  29.   }  
  30. });  
  31. </script>  
  32. </body>  
  33. </html>  

Test it Now

Difference between detach() and remove() method

Let's take an example to clear the difference between detach() and remove() method:

jQuery detach() example 3

  1. <!DOCTYPE html>  
  2. <html>  
  3. <head>  
  4. <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script>  
  5. <script>  
  6. $(document).ready(function(){  
  7.     $("#btn1").click(function(){  
  8.         $("body").append($("#p1").detach());  
  9.     });  
  10.     $("#btn2").click(function(){  
  11.         $("body").append($("#p2").remove());  
  12.     });  
  13.     $("p").click(function(){  
  14.         $(this).animate({fontSize: "+=1px"})  
  15.     });  
  16. });  
  17. </script>  
  18. </head>  
  19. <body>  
  20. <p id="p1"><b>This paragraph will keep its click event even after it is moved.</b></p>  
  21. <p id="p2">This paragraph will not keep its click event after it is moved.</p>  
  22. <button id="btn1">Detach and append p element</button>  
  23. <button id="btn2">Remove and append p element</button>  
  24. </body>  
  25. </html>  

Test it Now

By the above example, it is clear that jQuery detach() method doesn't remove inner data and events. In the above example, the click event is remained safe even after the detach() method is applied.