Walking the DOM
Browser Document: Walking the DOM
What does "Walking the DOM" mean in JavaScript?
View Answer:
console.log(document.documentElement); // console.logs [object HTMLHtmlElement] <html> node
What properties does a node have for "Walking the DOM"?
View Answer:
let node = document.body; // Start with the body element
// Traverse through each child node
for(let i = 0; i < node.childNodes.length; i++) {
console.log(node.childNodes[i]);
}
// Access first and last child
console.log(node.firstChild);
console.log(node.lastChild);
// Access the next sibling
console.log(node.nextSibling);
// Access the previous sibling
console.log(node.previousSibling);
This code will output each child node of the body, the first and last child of the body, and the next and previous sibling of the body (if they exist).
What is a Node in the context of the DOM?
View Answer:
// Access the body element which is a node in the DOM
let node = document.body;
// Log the node type - Element node should return 1
console.log(node.nodeType);
// Log the node name - should return "BODY"
console.log(node.nodeName);
// Access and log first child node - could be an element, text or comment
console.log(node.firstChild);
This code will output the type and name of the body element node, as well as the first child node of the body.
What are the main types of nodes in the DOM?
View Answer:
What does the parentNode property return?
View Answer:
<div id="parent">
<p id="child">Hello, world!</p>
</div>
You can use JavaScript to access the parent node of the <p>
element like this:
var child = document.getElementById("child");
console.log(child.parentNode);
This would return the <div>
element with the id of "parent", because that is the parent node of the <p>
element.
Remember, the parentNode
property is read-only, and will return null
if the node does not have a parent, such as in the case of the document
node.
What is the difference between childNodes and children properties?
View Answer:
Here's an example that illustrates the difference...
<div id="parent">
<!-- Comment -->
Text content
<p>Paragraph</p>
</div>
And the corresponding JavaScript:
let parent = document.getElementById('parent');
console.log(parent.childNodes);
// Returns a NodeList: [Comment, Text, HTMLParagraphElement]
console.log(parent.children);
// Returns an HTMLCollection: [HTMLParagraphElement]
As you can see, childNodes
includes the comment and text nodes, while children
only includes the <p>
element. This difference is very important and must be considered when traversing the DOM in your JavaScript code.
How would you access the first and last child of a node?
View Answer:
<div id="parent">
<!-- Comment -->
Text content
<p>First paragraph</p>
<p>Second paragraph</p>
</div>
And the corresponding JavaScript:
let parent = document.getElementById('parent');
console.log(parent.firstChild);
// Returns a Comment node: <!-- Comment -->
console.log(parent.firstElementChild);
// Returns an Element node: <p>First paragraph</p>
console.log(parent.lastChild);
// Returns an Element node: <p>Second paragraph</p>
console.log(parent.lastElementChild);
// Returns an Element node: <p>Second paragraph</p>
In most cases, when dealing with HTML elements, you'll probably want to use firstElementChild
and lastElementChild
to avoid getting unexpected text or comment nodes. However, it's good to be aware of both sets of properties and their differences.
How do you check if a node has child nodes?
View Answer:
let parent = document.getElementById('parent');
if (parent.hasChildNodes()) {
console.log('The parent node has child nodes.');
} else {
console.log('The parent node does not have child nodes.');
}
What are the topmost tree nodes available directly as document properties?
View Answer:
console.log(document.documentElement); // console.logs [object HTMLHtmlElement] <html> node
What does null mean or equate to in the DOM?
View Answer:
<html>
<head>
<script>
console.log('From HEAD: ' + document.body); // null, there's no <body> yet
</script>
</head>
<body>
<script>
console.log('From BODY: ' + document.body); // HTMLBodyElement, now it exists
</script>
</body>
</html>
What is the difference between Child nodes and Descendants in the DOM?
View Answer:
Can you explain what the firstChild and lastChild properties do on elements?
View Answer:
elem.childNodes[0] === elem.firstChild; // true
elem.childNodes[elem.childNodes.length - 1] === elem.lastChild; // true
What type of object structure are childNodes?
View Answer:
for (let node of document.body.childNodes) {
console.log(node); // shows all nodes from the collection
}
// Doesn't work returns undefined
console.log(document.body.childNodes.filter); // undefined (there's no filter method!)
// Solution: turn childNodes into an array
console.log(Array.from(document.body.childNodes).filter); // function
Is it possible to loop over node collections with a for…in loop?
View Answer:
// shows 0, 1, length, item, values, forEach, and more.
for (let prop in document.body.childNodes) console.log(prop);
Can you define what a sibling is in the DOM structure?
View Answer:
<html>
<head>
...
</head>
<body>
...
</body>
</html>
Are there properties that we can use to access any of the following and previous node siblings (Note: including text and comment nodes)? How do you access the parent node?
View Answer:
// parent of <body> is <html>
console.log(document.body.parentNode === document.documentElement); // true
// after <head> goes <body>
console.log(document.head.nextSibling); // HTMLBodyElement
// before <body> goes <head>
console.log(document.body.previousSibling); // HTMLHeadElement
There are times when we do not want to access the text and comment nodes. Is there a property we can use to access the next sibling element node?
View Answer:
Here's an example of how to use nextElementSibling:
<div id="parent">
<!-- Comment -->
Text content
<p id="first">First paragraph</p>
<p id="second">Second paragraph</p>
</div>
And the corresponding JavaScript:
let first = document.getElementById('first');
console.log(first.nextElementSibling);
// Returns an Element node: <p id="second">Second paragraph</p>
Is it possible that the parent element is not an element?
View Answer:
console.log(document.documentElement.parentNode); // document
console.log(document.documentElement.parentElement); // null
Besides the essential DOM elements, do elements provide additional properties based on their specific type?
View Answer:
<table id="table">
<tr>
<td>one</td>
<td>two</td>
</tr>
<tr>
<td>three</td>
<td>four</td>
</tr>
</table>
<script>
// get td with "two" (first row, second column)
let td = table.rows[0].cells[1];
td.style.backgroundColor = 'red'; // highlight it
</script>