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.
उदाहरण: 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.
उदाहरण: 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.
उदाहरण: 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.
उदाहरण: 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.
उदाहरण: DOM Manipulation Basics
document.body.style.color = "blue";
document.title = "Updated Title";
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