• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar

The Programming Expert

Solving All of Your Programming Headaches

  • HTML
  • JavaScript
  • jQuery
  • PHP
  • Python
  • SAS
  • Ruby
  • About
You are here: Home / jQuery / How to Use jQuery to Remove Element

How to Use jQuery to Remove Element

November 30, 2021 Leave a Comment

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:

<div>
<div id="div"></div>
<div id="other-div"></div>
</div>

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:

<div>
<div id="other-div"></div>
</div>

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.

<div id="div1">
  <p>This is the paragraph we will remove.</p>
  <div id="click-me">Remove paragraph</div>
</div>

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.

Remove paragraph

Full Code:


<div id="div1">
  <p>This is the paragraph we will remove.</p>
  <div id="click-me">Remove paragraph</div>
</div>

<script>

$("#click-me").click(function(){
    $("#div1 p").remove();
});

</script>

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 id="parent-div">
  <div id="div-1" class="remove-me">Div 1</div>
  <div id="div-2" class="remove-me">Div 2</div>
  <div id="div-3" class="remove-me">Div 3</div>
  <div id="click-me-1">Remove divs with class "remove-me"</div>
</div>

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:

Div 1
Div 2
Div 3
Remove divs with class “remove-me”

Full Code:

<div id="parent-div">
  <div id="div-1" class="remove-me">Div 1</div>
  <div id="div-2" class="remove-me">Div 2</div>
  <div id="div-3" class="remove-me">Div 3</div>
  <div id="click-me-1">Remove divs with class "remove-me"</div>
</div>

<script>

$("#click-me-1").click(function(){
  $(".remove-me").remove();
}); 

</script>

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:

<div id="parent-div" class="remove-me"></div>

Using the selector filter to remove an HTML element can be useful in certain cases.

Let’s say I have the following HTML:

<div id="parent-div-2">
  <div id="div-1" class="remove-me">Div 1</div>
  <div id="div-2" class="remove-me definitely-remove-me">Div 2</div>
  <div id="div-3" class="remove-me definitely-remove-me">Div 3</div>
  <div id="div-4" class="remove-me">Div 4</div>
  <div id="div-5" class="dont-remove-me definitely-remove-me">Div 5</div>
  <div id="click-me-2" class="click-me">Remove the divs with class "definitely-remove-me"</div><br>
</div>

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:

Div 1
Div 2
Div 3
Div 4
Div 5
Remove the divs with class “definitely-remove-me”

Full Code:

<div id="parent-div-2">
  <div id="div-1" class="remove-me">Div 1</div>
  <div id="div-2" class="remove-me definitely-remove-me">Div 2</div>
  <div id="div-3" class="remove-me definitely-remove-me">Div 3</div>
  <div id="div-4" class="remove-me">Div 4</div>
  <div id="div-5" class="dont-remove-me definitely-remove-me">Div 5</div>
  <div id="click-me-2" class="click-me">Remove the divs with class "definitely-remove-me"</div>
</div>

<script>

$("#click-me-2").click(function(){
  $(".remove-me").remove(".definitely-remove-me");
}); 

</script>

Hopefully this article has been useful for you to understand how to use jQuery to remove an element.

Other Articles You'll Also Like:

  • 1.  Using the jQuery fadeTo Method to Change the Opacity of an Element
  • 2.  Using jQuery to Set Visibility
  • 3.  Get the Height of a Div Using jQuery
  • 4.  Using jQuery to Add CSS Important Style
  • 5.  jQuery delay – How to Delay the Start of a Method
  • 6.  Using jQuery to Change Div Text
  • 7.  Using jQuery to Move Element Before Another
  • 8.  jQuery get img src – Get the Source of an Image Using jQuery
  • 9.  onclick this jQuery
  • 10.  Using jQuery to Detect Window Resize

About The Programming Expert

The Programming Expert is a compilation of a programmer’s findings in the world of software development, website creation, and automation of processes.

Programming allows us to create amazing applications which make our work more efficient, repeatable and accurate.

At the end of the day, we want to be able to just push a button and let the code do it’s magic.

You can read more about us on our about page.

Reader Interactions

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Primary Sidebar

About The Programming Expert

the programming expert main image

Welcome to The Programming Expert. We are a group of US-based programming professionals who have helped companies build, maintain, and improve everything from simple websites to large-scale projects.

We built The Programming Expert to help you solve your programming problems with useful coding methods and functions in various programming languages.

Search

Learn Coding from Experts on Udemy

Looking to boost your skills and learn how to become a programming expert?

Check out the links below to view Udemy courses for learning to program in the following languages:

Copyright © 2023 · The Programming Expert · About · Privacy Policy