The Document Object Model
Overview
The Document Object Model or DOM represents the logical tree structure of a document. The DOM allows us to connect to the document through programming languages. Essentially, the DOM empowers your code to interact with and control the web page in real-time, making it a cornerstone of modern web development.
"The DOM represents a document with a logical tree. Each branch of the tree ends in a node, and each node contains objects. DOM methods allow programmatic access to the tree. With them, you can change the document's structure, style, or content. Nodes can also have event handlers attached to them. Once an event is triggered, the event handlers get executed." - MDN Web Docs
Prepare
- Read: 📃 DOM (Document Object Model) - javascript.info
- Read: 📃 DOM tree - javascript.info
Activity Instructions
- Using the Walking the DOM document from javascript.info, complete the three (3) Tasks provided.
- Let us test the concept presented in the previous tasks about non-element nodes in the
DOM.
- Navigate to the Church website.
- Open up the browser's DevTools.
- Go to the console tab and clear the contents Ø.
- Write a for-of loop that will output to the console all the child nodes of the
document.body.
Hint
An example of how to do this is found in the Walking the DOM article under DOM collections. Make sure to output to the console and not to pop-up,alert boxes.
Code Hint
for (let node of document.body.childNodes) console.log(node)
- Run this loop in the console. Over 25 child nodes will be displayed.
- Find all the non-element nodes (#text).
Even though these #text nodes are not super interesting, you can use the console to view all sorts of information about each child node of the document.