How to remove elements with javascript

How to remove elements with javascript

To remove an HTML element, you must select the parent of the element and use the?removeChild(node) method.?

HTML File :

<!DOCTYPE html>

<html>

    <head>

        <title>Page Title</title>

    </head>

    <body>

        <div id="demo">

            <p id="p1">This is a paragraph.</p>

            <p id="p2">This is another paragraph.</p>

        </div>

    </body>

</html>
        

JS File :


//calling the function in window.onload to make sure the HTML is loaded

window.onload = function() {

    var parent = document.getElementById("demo");

    var child = document.getElementById("p1");

    parent.removeChild(child);

};
        

要查看或添加评论,请登录

社区洞察

其他会员也浏览了