How To Remove HTML Element Using jQuery

In this post, we will learn how to remove an HTML element using jQuery. For removing the element using jQuery, we can use element name, element id, element class etc. We can remove a single or multiple elements using jQuery remove() function. For this example, first, we need a JS file for accessing the remove() function. After setting the JS file, let's start coding.

First, we need to add the JavaScript file for accessing the remove() function.

  1. <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> 
Write the below example code for removing HTML element using different ways.
  1. <!DOCTYPE html>  
  2.   
  3. <html>  
  4. <head>  
  5.     <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>  
  6.     <script>  
  7.     $(document).ready(function () {  
  8.         $("#RemoveDiv").click(function () {  
  9.             $("#div1").remove();  
  10.         });  
  11.   
  12.         $("#RemoveDivclass").click(function () {  
  13.             $(".divclass").remove();  
  14.         });  
  15.   
  16.         $("#Removeh1").click(function () {  
  17.             $("h1").remove();  
  18.         });  
  19.     });  
  20.     </script>  
  21. </head>  
  22. <body>  
  23.     <div id="div1">  
  24.         I AM DIV TAG REMOVE USING ID  
  25.     </div>  
  26.   
  27.     <button id="RemoveDiv">Remove div using id</button>  
  28.   
  29.     <div class="divclass">  
  30.         I AM DIV TAG REMOVE USING CLASS  
  31.     </div>  
  32.   
  33.     <button id="RemoveDivclass">Remove div using class</button>  
  34.   
  35.     <h1>  
  36.         I AM H1 TAG REMOVE USING MY NAME  
  37.     </h1>  
  38.   
  39.     <button id="Removeh1">Remove h1 element using name</button>  
  40. </body>  
  41. </html> 
The above code will remove HTML elements in different ways, like when clicking on "Remove div using id" button that searches for "div1" id from HTML and removes the element. I click on "Remove div using class" button that searches ".divclass" from HTML and removes.
 
I click on "Remove h1 element using the name" and the button searches for "h1" from HTML and removes. So here, we removed the HTML element using ID, class name, or HTML element using jQuery.

See the below screenshot to see how the HTML looks.

output