The Wayback Machine - https://web.archive.org/web/20230724161514/https://www.geeksforgeeks.org/how-to-clear-all-divs-content-inside-a-parent-div/
Skip to content
Related Articles
Open In App

Related Articles

How to clear all div’s content inside a parent div ?

Improve Article
Save Article
Like Article
Improve Article
Save Article
Like Article

In this article, we are given an HTML document containing div elements and the task is to remove the existing HTML elements using jQuery. To remove elements and their content, jQuery provides two methods:

Example:

<div id="Parent">
   <div id="child1">child 1</div>
   <div id="child2">child 2</div>
   <div id="child3">child 3</div>
</div>

Syntax: The following function clears the parent “div”

$('#parent').empty();

The following function clears all the child “divs”, but leaves the parent intact.

$('#parent div').empty()

Example: In this example, we will clear all div’s content inside a parent div.

HTML




<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content=
        "width=device-width, initial-scale=1.0">
  
    <script src=
    </script>
</head>
  
<body>
    <div id="parent">
        Parent div
        <div id="child1">Child 1</div>
        <div id="child2">Child 2</div>
        <div id="child3">Child 3</div>
    </div>
      
    <button onclick=myFunc()>Click me</button>
  
    <Script>
        function myFunc() {
            $('#parent div').empty()
        }
    </Script>
</body>
</html>

Output:

 Clear all div's content inside a parent div

 Clear all div’s content inside a parent div


Last Updated : 18 Jan, 2023
Like Article
Save Article
Similar Reads
Related Tutorials