JavaScript - remove() method

revision:


Category : element

The Element.remove() method removes the element from the DOM.

Syntax :

        element.remove()
        node.remove()
    

Parameters:none

Examples:

            <p id="demo">Click "Remove", and this paragraph will be removed from the DOM.</p>
            <button onclick="myFunction()">Remove</button>
            <script>
                function myFunction() {
                const element = document.getElementById("demo");
                element.remove();
                }
            </script>
        

Practical examples

example: using remove.
Here is div-01
Here is div-02
Here is div-03
code:
                    <div>
                        <div id="div-01">Here is div-01</div>
                        <div id="div-02">Here is div-02</div>
                        <div id="div-03">Here is div-03</div>
                    </div>
                    <script>
                        const element = document.getElementById("div-02");
                        element.remove(); // Removes the div with the 'div-02' id
                        
                    </script>