• 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 Empty the Contents of an HTML Element

Using jQuery to Empty the Contents of an HTML Element

April 30, 2022 Leave a Comment

We can use the jQuery empty method to clear all of the child elements of an HTML div/element.

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

Let’s say I have the following HTML:

<div id="div1">
  <p>This is a paragraph with some text.</p>
  <p>This is a paragraph with some text.</p>
  <p>This is a paragraph with some text.</p>
  <p>This is a paragraph with some text.</p>
</div>

If we want to remove all of the paragraphs from the div, #div1, we can use the jQuery empty() method to do this with the following jQuery code.

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

The resulting HTML would be as follows:

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

If you are using WordPress, don’t forget to change the $ to jQuery as below:

jQuery("#div1").empty();

Using the jQuery empty() method to remove all elements from a Div with a click

We can remove all elements from a specific HTML element using jQuery very easily by combining the empty() 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 all paragraphs and divs from the main div.


<style>#div1 { border: 1px solid #000; padding: 10px; } .boxes { width: 200px; height: 200px; margin-bottom: 10px; background: #82b5be; }</style>
<div id="div1">
  <p>This is the paragraph we will remove.</p>
  <p>This is the paragraph we will remove.</p>
  <div class="boxes">This box will be removed.</div>
  <p>This is the paragraph we will remove.</p>
  <p>This is the paragraph we will remove.</p>
</div>
<div id="click-me">Empty div above</div>

Below is the JavaScript code which will allow the user to be able div, #div1:

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

The final code and output for this example of how to empty a specific div using jQuery and JavaScript is below:

Code Output:

This is the paragraph we will remove.

This is the paragraph we will remove.

This box will be removed.

This is the paragraph we will remove.

This is the paragraph we will remove.

Empty div above

Full Code:


<style>#div1 { border: 1px solid #000; padding: 10px; } .boxes { width: 200px; height: 200px; margin-bottom: 10px; background: #82b5be; }</style>
<div id="div1">
  <p>This is the paragraph we will remove.</p>
  <p>This is the paragraph we will remove.</p>
  <div class="boxes">This box will be removed.</div>
  <p>This is the paragraph we will remove.</p>
  <p>This is the paragraph we will remove.</p>
</div>
<div id="click-me">Empty div above</div>

<script>

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

</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.  jQuery clone – Making a Copy of an Element
  • 2.  Using jQuery to Move Element Before Another
  • 3.  Using jQuery to Add Required Attribute to Input Field
  • 4.  Using jQuery to Get the Next Sibling of an Element
  • 5.  Using the jQuery fadeTo Method to Change the Opacity of an Element
  • 6.  Using jQuery to Remove All Options From Select
  • 7.  Using jQuery to Change Background Color
  • 8.  Toggle the Visibility of an Element Using jQuery
  • 9.  jQuery mousedown – An Interactive Example of the jQuery mousedown Method
  • 10.  Using jQuery to Get the Bottom Position of 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