jQuery remove()

jQuery remove() method is used to remove existing HTML elements from DOM.

To remove existing HTML elements using jquery,
  • Select HTML elements to be removed using jQuery Select.
  • Call remove method on the selection to remove those HTML elements from DOM.

Syntax - jQuery remove()

To remove the HTML element permanently from DOM using jQuery, use remove() method on the selection. Following is the syntax of jQuery remove() method :

 $("htmlElement").remove();

Examples - jQuery remove()

Following examples help you understand the usage of jQuery remove() method :

Example - Remove HTML elements based on class

In the following example, on clicking a button, using jQuery remove() method, we remove HTML elements whose class is abc.

<!DOCTYPE html>
<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script>
$(document).ready(function(){
 $("#btnAct").click(function(){
  $(".abc").remove();
 });
});
</script>
</head>
<body>

 <h2>Heading 2</h2>
 
 <p class="abc">A paragraph.</p>
 <p>Another paragraph.</p>

 Some List
 <ul>
  <li class="abc">Item 1</li>
  <li>Item 2</li>
 </ul>

 <button id="btnAct">Remove elements with abc class</button>

</body>
</html>
Try Online

Example - Remove div elements based on ID

Following example demonstrates how to remove selected DIV elements based on id.

<!DOCTYPE html>
<html>
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script>
$(document).ready(function(){
 $("#btnAct").click(function(){
  $("div#desc").remove();
 });
});
</script>
</head>
<body>

 <h2>Heading 2</h2>
 
 <div id="desc" style="background:#DDD;border:1px solid #CCC;padding:20px;margin:10px;">
 <p class="abc">A paragraph.</p>
 <p>Another paragraph.</p>
 </div>

 Some List
 <ul>
 <li class="abc">Item 1</li>
 <li>Item 2</li>
 </ul>

 <button id="btnAct">Remove div with id=desc</button>

</body>
</html>
Try Online

Conclusion

In this jQuery Tutorial, we have learnt jQuery remove() method : Syntax and Usage with examples.