How to remove DOM element in JavaScript? [SOLVED]


Written by - Olorunfemi Akinlua
Reviewed by - Deepak Prasad

JavaScript, the backbone of interactive web experiences, is a powerful tool that helps developers to create dynamic web applications. One essential aspect of this dynamism is the ability to manipulate the Document Object Model (DOM) - the hierarchical structure that represents the content and structure of an HTML document. At times, it becomes crucial to remove or replace elements within the DOM to facilitate a seamless user experience.

In this article, we will explore various techniques to remove DOM elements in JavaScript, providing you with a comprehensive understanding of the process. From selecting the target element to using native JavaScript methods and leveraging popular libraries like jQuery, we will examine different approaches to efficiently and effectively remove elements from the DOM.

Whether you are a beginner starting your journey in web development or an experienced developer looking to brush up on your DOM manipulation skills, this guide will serve as a valuable resource in helping you harness the power of JavaScript to create dynamic and engaging web applications. So, let's dive in and learn how to remove DOM elements in JavaScript!

 

Different methods to remove DOM element using JavaScript

JavaScript is a client-side scripting language used to add interactivity to web pages. One of the main tasks in JavaScript is to manipulate the Document Object Model (DOM), which is a hierarchical tree structure of all the HTML elements on a web page.

One common DOM manipulation task is removing elements from the DOM. For different reasons, from interactivity to change propagation, we might want to remove an element using JavaScript. In this article, we will discuss two methods for removing elements in the DOM: the removeChild() method and the remove() method. These methods are built-in and provide an easy way to achieve such tasks.

 

Method-1: Using the removeChild() method to remove an element

The first approach is to use the removeChild() method. The removeChild() method is used to remove a child node from a parent node. It takes one argument: the node to be removed.

Here is the syntax for the removeChild() method

parentNode.removeChild(node);

To showcase how the removeChild() method, we are going to remove a paragraph element with some text we feel is no longer relevant. This paragraph element has a subtext identifier so we will make use of the getElementById() method to select it. Afterwards, we reference it as the parentNode and use the removeChild() method to remove it.

<!DOCTYPE html>
<html lang="en">
    <head>
        <meta charset="UTF-8" />
        <meta http-equiv="X-UA-Compatible" content="IE=edge" />
        <meta name="viewport" content="width=device-width, initial-scale=1.0" />
        <title>JS Approach</title>
    </head>
    <body>
        <h1 id="old">how to remove an element in js</h1>
        <p id="subtext">Let's show you how to remove that element</p>
        <div class="main">
            <p>We can make use of two methods to remove elements;</p>
            <ul id="inside">
                <li>removeChild()</li>
                <li>remove()</li>
            </ul>
        </div>
    </body>
    <script src="script.js"></script>
</html>

The script.js file

let node = document.getElementById("subtext");
let parentNode = node.parentNode;
parentNode.removeChild(node);

Output

how to remove an element in js

 

Method-2: Using the remove() method to remove an element

Another approach we can make use of is the remove() method. The remove() method is a more modern and concise way to remove an element from the DOM. It is available in modern browsers and can be used as an alternative to removeChild().

Here is the syntax for using the remove() method.

node.remove();

Where the node is the node/element we want to remove the DOM structure.

Let’s illustrate how the remove() method works, and remove the same paragraph as in the previous section. We will see that the entire approach is simpler and concise and require lesser lines. The script.js file will contain just the following code statements.

let node = document.getElementById("subtext");
node.remove();

Output

how to remove an element in js

 

Method-3: Remove an element without removing its children

To remove an element without removing its children, you can use the childNodes property which returns a live NodeList of the child nodes of the selected element. As a result, we can obtain all the child nodes and replace the parent element/node using the replaceWith() method.

To illustrate this, we will make use of the same HTML page as in the first section, and remove the div with the main class and keep the p and ul elements. Our script.js file will contain the following code.

const main = document.querySelector(".main");
main.replaceWith(...main.childNodes);

Output

how to remove an element in js

Though, the rendered webpage is the same, the markup of the HTML page will look like the below

<!DOCTYPE html>
<html lang="en">
    <head>
        <meta charset="UTF-8" />
        <meta http-equiv="X-UA-Compatible" content="IE=edge" />
        <meta name="viewport" content="width=device-width, initial-scale=1.0" />
        <title>JS Approach</title>
    </head>
    <body>
        <h1 id="old">how to remove an element in js</h1>
        <p id="subtext">Let's show you how to remove that element</p>
        <p>We can make use of two methods to remove elements;</p>
        <ul id="inside">
            <li>removeChild()</li>
            <li>remove()</li>
        </ul>

    </body>
    <script src="script.js"></script>
</html>

 

Summary

In this article, we discussed two methods for removing elements from the DOM: the removeChild() method and the remove() method. Both methods allow you to remove elements from the DOM, but the remove() method is more modern and concise.

When removing elements from the DOM, it's important to understand the difference between the two methods and choose the one that best fits your needs. If you need to remove an element without removing its children, you can use the childNodes property to obtain the child elements and replace the parent element with the replaceWith() method.

Being able to remove elements from the DOM is a powerful tool for adding interactivity to web pages and improving user experience. Whether you're using the removeChild() method or the remove() method, understanding how to remove elements in the DOM is essential for working with JavaScript and the DOM.

 

References

Element.remove() - Web APIs | MDN (mozilla.org)
Element.replaceWith() - Web APIs | MDN (mozilla.org)

 

Olorunfemi Akinlua

He is boasting over five years of experience in JavaScript, specializing in technical content writing and UX design. With a keen focus on programming languages, he crafts compelling content and designs user-friendly interfaces to enhance digital experiences across various domains. You can connect with him on LinkedIn.

Can't find what you're searching for? Let us assist you.

Enter your query below, and we'll provide instant results tailored to your needs.

If my articles on GoLinuxCloud has helped you, kindly consider buying me a coffee as a token of appreciation.

Buy GoLinuxCloud a Coffee

For any other feedbacks or questions you can send mail to admin@golinuxcloud.com

Thank You for your support!!

Leave a Comment