JS DOM
In this page:
What Is the DOM
The DOM is a live, in-memory tree representation of an HTML page, where every element, attribute, and piece of text becomes a connected node that JavaScript can read and manipulate directly.
Note: The DOM is not the same as the raw HTML file, it's a dynamic structure the browser builds and keeps updated as JavaScript changes the page.
Warning: Because the DOM can differ from the original HTML after JavaScript runs, viewing a page's source in some tools shows the original file, not the current live DOM.
Example: What Is the DOM
console.log(document); // the live in-memory tree of the page
The document Object
document is the built-in object representing the entire loaded page, serving as the starting point for nearly every DOM operation, finding elements, reading properties, or creating new content.
Note: document.title and document.URL are quick, commonly used properties for reading basic information about the current page.
Warning: document only reflects the page it's running on, scripts cannot use it to reach into or modify a completely different page or origin.
Example: The document Object
console.log(document.title);
console.log(document.body);
The DOM Tree Structure
The DOM organizes a page hierarchically, document at the very top, followed by html, then head and body, and every nested element continuing that parent-child relationship all the way down to individual text content.
Note: Understanding parent, child, and sibling relationships in the DOM tree helps a lot when navigating or manipulating nearby elements relative to one you already have.
Warning: Even whitespace and line breaks in your HTML can become their own text nodes in the DOM tree, which occasionally affects child counting.
Example: The DOM Tree Structure
// document -> html -> head/body -> nested elements -> text
console.log(document.documentElement.tagName); // "HTML"
Selecting Elements (Preview)
Before you can manipulate an element, you first need to select it from the DOM, using methods like getElementById() or querySelector(), which are covered in full depth in the next chapter on DOM methods.
Note: Selecting an element doesn't change anything on the page by itself, it simply gives your script a reference to work with afterward.
Warning: Trying to select an element that doesn't exist yet, before it's been added to the page, returns null rather than the element you expected.
Example: Selecting Elements (Preview)
const el = document.getElementById("demo"); // preview of the next chapter
console.log(el);
DOM Manipulation Basics
Once an element is selected, JavaScript can change its text, attributes, styles, or structure directly, and these changes appear instantly on the page, with no reload required, this is the foundation of interactive web pages.
Note: Even a single line of JavaScript, like changing textContent, is technically DOM manipulation, the same underlying mechanism behind far more complex interactive features.
Warning: DOM manipulation changes are visible instantly, but they exist only in the browser's memory, refreshing the page discards them unless they're saved somewhere.
Example: DOM Manipulation Basics
document.body.style.color = "blue";
document.title = "Updated Title";
- Trying to access DOM elements before the page has finished loading them, resulting in null instead of the expected element.
- Confusing the DOM tree with the original HTML source, the DOM can be modified by JavaScript and no longer match the initial HTML exactly.
- Assuming every visible change requires reloading the page, DOM manipulation updates what's on screen instantly, without any reload.
- The DOM represents an HTML page as a tree of connected node objects that JavaScript can read and modify.
- The document object is the entry point for accessing and interacting with the entire DOM tree.
- DOM manipulation, changing text, attributes, or structure through JavaScript, is what makes pages feel dynamic and interactive.
The DOM and the document object are core, universal web platform features supported identically in every browser.
Chapter Quiz — Complete all 26 topics to unlock
0/26 topics done
Complete these topics first:
- JS Dates
- JS Math
- JS Conditionals
- JS Switch
- JS Loop For
- JS Loop While
- JS Iterables
- JS Sets
- JS Maps
- JS typeof
- JS Type Conversion
- JS Destructuring
- JS Arrow Functions
- JS Classes
- JS Modules
- JS Promises
- JS Async/Await
- JS DOM
- JS DOM Methods
- JS Events Advanced
- JS DOM Navigation
- JS DOM Collections
- JS Async Callbacks
- JS Async Parallel
- JS Date Set
- JS Set Logic