JS DOM
In this page:
document.getElementById("id");
document.querySelector("selector");
element.textContent = value;
element.style.property = value;
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.
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.
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.
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.
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.
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