• 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 / Using jQuery to Delete an Element

Using jQuery to Delete an Element

March 15, 2022 Leave a Comment

To use jQuery to delete an element, the simplest way is to use the jQuery remove() method.

$("#div1").remove();

Let’s say I have the following HTML:

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

If we want to delete #div1 from the DOM, we can use the jQuery remove() method to do this with the following JavaScript code.

$("#div1").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("#div1").remove()

Deleting an HTML Element Using jQuery With a Click

We can delete 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 delete the paragraph from the div.

<div id="div1">
  <p>This paragraph will not be deleted.</p>
  <p>This is the paragraph we will delete.</p>
  <div id="click-me">Delete paragraph</div>
</div>

Below is the JavaScript code which will allow the user to be able to delete the paragraph:

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

We need to be careful when we are deleting elements from the DOM. We could have used the following code as well to delete 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.

Finally, you will notice we use the jQuery :nth-child() selector. This is so we only target the second paragraph in the div, as we don’t want to delete the first paragraph.

The final code and output for this example of how to use jQuery to delete an element with a click is below:

Code Output:

This paragraph will not be deleted.

This is the paragraph we will delete.

Delete paragraph

Full Code:

<div id="div1">
  <p>This paragraph will not be deleted.</p>
  <p>This is the paragraph we will delete.</p>
  <div id="click-me">Delete paragraph</div>
</div>

<script>

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

</script>

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

Other Articles You'll Also Like:

  • 1.  jQuery text – Set the text of an HTML Element
  • 2.  jQuery blur – How to Use the blur() Method on an HTML Form
  • 3.  jQuery has – Check if an Element Has Other Elements
  • 4.  jQuery on change
  • 5.  Using jQuery to Change the Text Color of a Paragraph
  • 6.  Using jQuery to Replace a Class
  • 7.  Using jQuery to Select Multiple Classes
  • 8.  Using jQuery to Change Background Color
  • 9.  Using jQuery to Get Element by ID
  • 10.  Using jQuery to Remove Attribute from HTML Element

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