By the end of this lesson, learners will be able to:
document objectThe DOM (Document Object Model) is a programmatic representation of a webpage. It turns HTML into a tree-like structure that JavaScript can access, modify, and interact with.
When a browser loads a webpage:
Think of the DOM as a digital map of your HTML, where each element is a node you can find, change, or remove with JavaScript.
Given this HTML:
<html>
<body>
<h1>Hello, world!</h1>
<p>This is a paragraph.</p>
</body>
</html>
The DOM structure (tree) looks like:
document
└── html
└── body
├── h1 → "Hello, world!"
└── p → "This is a paragraph."
Each HTML tag becomes a node in the tree, and content inside the tag is a text node.
The DOM allows JavaScript to:
Without the DOM, a webpage would be static and unresponsive.
JavaScript accesses the DOM through the document object, which represents the entire HTML document.
console.log(document);
This prints out the full HTML structure in the browser console.
You can use document to:
getElementById, querySelector, etc.)HTML:
<p id="message">Welcome!</p>
JavaScript:
document.getElementById("message").textContent = "Hello from JavaScript!";
✅ This uses the DOM to find the paragraph and change its content — without reloading the page!
| Term | Description |
|---|---|
| Node | Any item in the DOM tree (element, text, attribute, etc.) |
| Element | An HTML tag like <p>, <div>, <button>, etc. |
| Text Node | The text inside an element |
| Root Node | The top-level node (document) |
| Parent/Child | Elements that contain or are contained by others |