To remove a HTML element using jQuery, the simplest way is to use the remove() method.
$("#div").remove();
Let’s say I have the following HTML:
If we want to remove #div from the DOM, we can use the jQuery remove() method to do this with the following JavaScript code.
$("#div").remove();
The resulting HTML would be as follows:
If you are using WordPress, don’t forget to change the $ to jQuery as below:
jQuery("#div").remove()
Removing an HTML Element Using jQuery With a Click
Many times when creating a web page and the user experience, we want to remove certain elements based on an interaction with another element on the web page.
We can remove a specific HTML element using jQuery very easily by combining the remove() method with a click event.
Let’s say we have the following HTML code and we want to give the user the ability to remove the paragraph from the div.
This is the paragraph we will remove.
Remove paragraph
Below is the JavaScript code which will allow the user to be able to remove the paragraph:
$("#click-me").click(function(){
$("#div1 p").remove();
});
We need to be careful when we are removing elements from the DOM. We could have used the following code as well to remove the paragraph, but this would have also affected all the other paragraphs on the web page.
$("#click-me").click(function(){
$("p").remove();
});
In any case, we need to be careful with our selectors when using jQuery.
The final code and output for this example of how to remove a specific paragraph using jQuery and Javascript is below:
Code Output:
This is the paragraph we will remove.
Full Code:
This is the paragraph we will remove.
Remove paragraph
Using jQuery to Remove Multiple HTML Elements
We can use the jQuery removeClass() method to remove multiple elements very easily.
The key to removing multiple HTML elements is understanding how to select the appropriate elements in jQuery.
For example, if we want to remove all divs from our page, we could do the following:
$("div").remove();
This would most likely wipe out the entire page, since the majority of HTML elements are divs. Instead, we should be more precise with our selector.
Let’s say we have the following HTML:
Div 1
Div 2
Div 3
Remove divs with class "remove-me"
If we want to remove all of the divs with class “remove-me”, we just need to do the following in our Javascript code:
$("#click-me-1").click(function(){
$(".remove-me").remove();
});
If we had other divs with class “remove-me” in our web page, but didn’t want to remove them, we would want to use the following Javascript instead:
$("#click-me-1").click(function(){
$("#parent-div .remove-me").remove();
});
In either case, the divs with “remove-me” will be removed from the parent div.
The final code and output for this example of how to remove multiple classes using jQuery and JavaScript is below:
Code Output:
Full Code:
Div 1
Div 2
Div 3
Remove divs with class "remove-me"
jQuery Remove Element Selector Not Working
The jQuery remove() method allows us to pass a filter to it which will allow us to filter the selector.
In the example above where we were trying to remove all of the divs with class “remove-me”, we could have done the following:
$("#click-me-1").click(function(){
$("div").remove(".remove-me");
});
This is a little bit confusing – the filter is directly on the selector $(“div”) and NOT on the parent element.
For example, if you try to do the following, this will NOT work:
$("#click-me-1").click(function(){
$("#parent-div").remove(".remove-me");
});
This will not work because we don’t have any elements which look like:
Using the selector filter to remove an HTML element can be useful in certain cases.
Let’s say I have the following HTML:
Div 1
Div 2
Div 3
Div 4
Div 5
Remove the divs with class "definitely-remove-me"
We want to only remove the divs which have class “remove-me” and “definitely-remove-me”.
We can do this using the selector filter of the remove() method.
$("#click-me-2").click(function(){
$(".remove-me").remove(".definitely-remove-me");
});
In this case, we will be left with Div 1, Div 4, and Div 5.
The final code and output for this example of how to remove multiple classes using the selector filter with jQuery and JavaScript is below:
Code Output:
Full Code:
Div 1
Div 2
Div 3
Div 4
Div 5
Remove the divs with class "definitely-remove-me"
Hopefully this article has been useful for you to understand how to use jQuery to remove an element.