DOM Basics
Understand the Document Object Model (DOM)! Learn how JavaScript interacts with HTML to create dynamic web pages.
Understand the Document Object Model (DOM)! Learn how JavaScript interacts with HTML to create dynamic web pages. This hands-on tutorial focuses on practical implementation of dom basics concepts.
DOM Basics
Welcome to the browser! Until now, we've been writing JavaScript logic. Now, we'll use JavaScript to control the web page itself.
1. What is the DOM? π³
What is it? DOM stands for Document Object Model. It is a tree-like representation of your HTML structure.
Why use it? HTML is just static text. The DOM is a "live" object that JavaScript can interact with. It allows you to:
- Change text and content.
- Modify styles (colors, fonts).
- Add or remove HTML elements.
- Respond to user clicks.
How it works: When a browser loads a web page, it converts your HTML into the DOM Tree.
2. The document Object π
The document object is your main entry point to the DOM. It represents the entire web page.
3. DOM Nodes πΏ
Everything in the DOM is a Node. There are different types:
- Element Nodes: The HTML tags (e.g.,
<div>,<h1>,<p>). - Text Nodes: The actual text inside the tags (e.g., "Hello World").
- Attribute Nodes: The attributes on tags (e.g.,
class="container",src="image.jpg").
Navigating the Tree: You can move up, down, and sideways in the tree.
4. Window vs. Document πͺ
It's easy to confuse them!
window: Represents the browser window (tabs, history, scrolling). It is the global object.document: Represents the web page content (HTML). It is a property of the window (window.document).
Practical Example: Inspecting the DOM π
Open your browser's Developer Tools (F12 or Right Click -> Inspect).
- Go to the Elements tab: This IS the DOM!
- Go to the Console tab: Type
document.body.style.background = "red"and hit Enter. You just manipulated the DOM!
AI Mentor
Confused about "Document Object Model (DOM), DOM tree structure, nodes, and the document object"? Ask our AI mentor for a simplified explanation.
Quiz
Quiz
Question 1 of 4What does DOM stand for?
Key Takeaways
β
DOM turns HTML into a tree of objects.
β
document is the root of the page content.
β
Nodes are the individual parts (Elements, Text).
β
window is the browser; document is the page.
What's Next?
Now that we know what the DOM is, let's learn how to Select and Modify Elements to change the page!
Keep coding! π