How to get all the HTML body element children in JavaScript

1 Answer

0 votes
<!DOCTYPE html>
<html>
    <body>
        <h1>H1</h1>
        <p>P1</p>
        <p id="p_id">P2</p>
        <span id="span_id"></span>
        <script>
            var bc = document.body.children;
            var txt = "";
            for (var i = 0; i < bc.length; i++) {
                txt = txt + bc[i].tagName + "<br>";
                document.getElementById("span_id").innerHTML = txt;
            }
        </script>

    </body>
</html>


<!--

H1

P1

P2
H1
P
P
SPAN
SCRIPT

-->

 



answered Oct 26, 2019 by avibootz
...