Accessing Children of an Element

 <!DOCTYPE html>

<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Accessing Children of an Element</title>
</head>

<body>
    <div>
        <p>This is me and I am great</p>
        <span>Siblings</span>
    </div>
    <h1>Accessing Children of an Element</h1>
    <script src="../Accessing_Children_of_an_Element.js"></script>
</body>

</html>





Below File is Accessing_Children_of_an_Element.js File.
console.log(document.body.firstChild);  //Here, firstChild is textNode
console.log(document.body.lastChild);
console.log(document.body.childNodes);
// Following is always true:
//elem.childNodes[0] == elem.firstChild
//elem.childNodes[elem.childNodes.length - 1] === elem.lastChild

// There is also a method elem.hasChildNodes() to check whether there are any child nodes.
// NOTE: childNodes looks like an array but if not actually an array but a collection we can use
//Array.from(collection) to convert it into an Array.(Array methods won't work)
let arr = Array.from(document.body.childNodes);
console.log(arr);





Comments

Popular posts from this blog

Generators tutorial in Advance JavaScript

Document Object Module DOM querySelector and querySelectorAll tutorial in JavaScript

Find Even and Odd Numbers with Loops tutorial in JavaScript