The Wayback Machine - https://web.archive.org/web/20240118175837/https://www.geeksforgeeks.org/how-to-remove-specific-div-element-by-using-javascript/
Open In App
Related Articles

How to remove specific div element by using JavaScript?

Improve
Improve
Improve
Like Article
Like
Save Article
Save
Report issue
Report

In this article, we will discuss three simple ways to remove a specific ‘div’ element using plain JavaScript.

These are the following methods:

Using parentNode.removeChild() method

This method removes a specified child node from the DOM tree and returns the removed node. 

Syntax:  

element.parentNode.removeChild(element);

Example: This example uses the parentNode.removeChild() method to remove a specific ‘div’ element. 

html




<!DOCTYPE html>
<html>
 
<head>
    <title>
        Remove specific divisible
        element using Javascript
    </title>
</head>
 
<body style="text-align: center;">
    <h1 style="color: green;">
        GeekforGeeks
    </h1>
    <div id="gfg_up" style="font-size: 15px;
                    font-weight: bold;">
        A Computer Science Portal for Geeks
    </div>
    <br />
    <button onclick="GFG_click()">
        click to remove
    </button>
    <hr />
    <div id="gfg_down" style="font-size: 15px;
                    font-weight: bold;">
        A Computer Science Portal for Geeks
    </div>
 
    <script type="text/javascript">
        function GFG_click() {
            let gfg_down =
                document.getElementById("gfg_down");
            gfg_down.parentNode.removeChild(gfg_down);
        }
    </script>
</body>
 
</html>


Output:

Using outerHTML property

The outerHTML property is used to set the contents of the HTML element. Hence we can remove a specified ‘div’ element by setting its contents to “” using the outerHTML property. 

Syntax:  

element.outerHTML=""

Example: This example uses the outerHTML attribute to remove a specific ‘div’ element.  

html




<!DOCTYPE html>
<html>
 
<head>
    <title>
        Remove specific divisible
        element using Javascript
    </title>
</head>
 
<body style="text-align: center;">
    <h1 style="color: green;">
        GeekforGeeks
    </h1>
    <div id="gfg_up" style="font-size: 15px;
                    font-weight: bold;">
        A Computer Science Portal for Geeks
    </div>
    <br />
    <button onclick="GFG_click()">
        click to remove
    </button>
    <hr />
    <div id="gfg_down" style="font-size: 15px;
                    font-weight: bold;">
        A Computer Science Portal for Geeks
    </div>
 
    <script type="text/javascript">
        function GFG_click() {
            document.getElementById("gfg_down")
                .outerHTML = "";
        }
    </script>
</body>
 
</html>


Output:

Using .remove() method

This method removes the specified div element and all its child nodes. 

Syntax: 

element.remove();

Example: This example uses the remove() method to remove a specific ‘div’ element.  

html




<!DOCTYPE html>
<html>
 
<head>
    <title>
        Remove specific divisible
        element using Javascript
    </title>
</head>
 
<body style="text-align: center;">
    <h1 style="color: green;">
        GeekforGeeks
    </h1>
    <div id="gfg_up" style="font-size: 15px;
                    font-weight: bold;">
        A Computer Science Portal for Geeks
    </div>
    <br />
    <button onclick="GFG_click()">
        click to remove
    </button>
    <hr />
    <div id="gfg_down" style="font-size: 15px;
                    font-weight: bold;">
        A Computer Science Portal for Geeks
    </div>
    <script type="text/javascript">
        function GFG_click() {
            let gfg_down =
                document.getElementById("gfg_down");
            gfg_down.remove();
        }
    </script>
</body>
 
</html>


Output:


Learn to code easily with our course Coding for Everyone. This course is accessible and designed for everyone, even if you're new to coding. Start today and join millions on a journey to improve your skills!

Whether you're preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape, GeeksforGeeks Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we've already empowered, and we're here to do the same for you. Don't miss out - check it out now!

Commit to GfG's Three-90 Challenge! Purchase a course, complete 90% in 90 days, and save 90% cost click here to explore.
Last Updated : 20 Dec, 2023
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads
Complete Tutorials