When a browser loads a page, it does not keep the HTML text. It builds a structure of objects in memory, and JavaScript works on that structure. This structure is the Document Object Model.
The DOM Tree
The DOM is a tree of objects that represents the page. The document object is the root. Below it is the html element, which has two children: head and body. Every element inside them is another node in the tree. Family words describe the relationships: an element directly inside another is its child, the outer element is its parent, and elements with the same parent are siblings.
Definition: DOM. The Document Object Model, a tree of objects that represents the page in the browser.
Kinds of Nodes
Each item in the tree is a node. Element nodes represent tags. Text nodes hold the words inside elements. Attribute nodes hold values such as id and href. Even the whitespace and comments in the file can become nodes, although scripts rarely need them.
Definition: DOM node. One item in the DOM tree, such as an element, an attribute or a piece of text.
Table 3.6: Main kinds of DOM node
| Node | Represents | Example |
|---|---|---|
| Document | The whole page | document |
| Element | A tag | h1, p, div |
| Text | Text inside an element | Hello |
| Attribute | A value in a tag | id="title" |
Window and Document
The window object stands for the browser window and is the top-level object of a script. The document object is the page that is loaded in that window. Properties such as document.title and document.body give access to parts of the page, and the methods of the next topics find other elements.
The DOM Is Live
The DOM is not the same as the HTML file. The file stays the same, while the DOM changes whenever a script changes it. The browser redraws the screen at once to match, which is why a script can update a page without reloading it.
Example 3.6: Reading the DOM tree
The page below has a heading and a paragraph in its body:
<body>
<h1>Hi</h1>
<p>Text</p>
</body>
In the DOM, body has two child element nodes, h1 and p, and they are siblings. The h1 node has one child, a text node that holds the word Hi. A script can read these parts:
<h1 id="t">Hi</h1>
<p id="out"></p>
<script>
document.getElementById("out").textContent =
"Page title: " + document.title +
" | body children: " + document.body.children.length;
</script>
Output in the browser:
Common mistakes
- Thinking the DOM is the HTML file, when it is a live tree that scripts can change.
- Forgetting that text inside an element is a separate text node.
- Confusing window, the browser window, with document, the page.
- Expecting a change made through the DOM to appear in the saved HTML file.
Key takeaways
- The DOM is a tree of nodes that represents the loaded page.
- The document is the root, and relationships are parent, child and sibling.
- Elements, text and attributes are different kinds of node.
- Changes to the DOM show on the screen at once, and the file is unchanged.
Practice questions
- Which object is the root of the DOM tree?
- What are two elements with the same parent called?
- Is the DOM the same as the HTML file?
Answers to the practice questions
- The document object.
- Siblings.
- No. The DOM is a live tree built from the file, and scripts can change it.
Sources
- Official BCA syllabus (NEP 2025 scheme), Web Application Development, course code 25BCA405DS04, Unit III
