Lesson 4.6: DOM (Document Object Model) Basics
The Document Object Model (DOM) is a programmatic representation of a web page that allows JavaScript to access, modify, and manipulate HTML elements. Essentially, the DOM turns every element, attribute, and text in a webpage into objects that can be controlled via JS.
1. What is DOM?
-
DOM is a tree-like structure representing the HTML document.
-
Each HTML element is a node in the tree.
-
JavaScript can read or modify nodes dynamically.
Example HTML:
DOM Representation:
2. Accessing the DOM
JavaScript provides multiple ways to select elements:
a) By ID
b) By Class Name
c) By Tag Name
d) Query Selector
-
Selects first match:
-
Selects all matches:
3. Why DOM is Important
-
Allows dynamic content updates (change text, add/remove elements).
-
Handles user interactions (clicks, input, hover).
-
Forms the foundation for modern front-end frameworks like React, Angular, and Vue.
✅ Summary:
-
DOM = representation of HTML as objects.
-
Every element is a node that JS can access and manipulate.
-
Use
getElementById,getElementsByClassName,getElementsByTagName,querySelector,querySelectorAllto select elements. -
DOM manipulation enables interactive and dynamic web pages.
