jQuery remove()

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

jQuery remove()

The jQuery remove() method is used to remove the selected elements out of the DOM. It removes the selected element itself, as well as everything inside it (including all texts and child nodes). This method also removes the data and the events of the selected elements.

If you want to remove elements without removing data and events, you should use the detach() method. If you want to remove only data and events, use the empty() method.

Syntax:

  1. $(selector).remove(selector)  

Parameters of jQuery remove() method:

Parameter Description
Selector is an optional parameter. It specifies whether to remove one or more elements. If you have to remove more than one element then you should separate them with comma (,).

Example of jQuery remove() method

  1. <!doctype html>  
  2. <html lang="en">  
  3. <head>  
  4.   <meta charset="utf-8">  
  5.   <title>remove demo</title>  
  6.   <style>  
  7.   p {  
  8.     background: pink;  
  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. This is javatpoint.com<br/>  
  17. <p>A place for all technology.</p>  
  18. <button>Execute remove() method on paragraphs</button>  
  19.  <script>  
  20. $( "button" ).click(function() {  
  21.   $( "p" ).remove();  
  22. });  
  23. </script>  
  24. </body>  
  25. </html>  

Test it Now

jQuery remove() example 2

  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").remove();  
  9.     });  
  10. });  
  11. </script>  
  12. </head>  
  13. <body>  
  14. <p>Welcome Guys!</p>  
  15. <p><b>This is javatpoint.com</b></p>  
  16. <button>Click here to execute remove() method</button>  
  17. </body>  
  18. </html>  

Test it Now