๐ฏ Learning Objectives
- Understand the DOM (Document Object Model)
- Select HTML elements with JavaScript
- Change HTML content dynamically
- Modify CSS styles with JavaScript
- Build interactive web pages
๐ณ What is the DOM?
DOM (Document Object Model) is a programming interface that represents your HTML document as a tree structure. JavaScript can access and change all elements, attributes, and styles.
๐ฏ Selecting Elements
getElementById()
// HTML <h1 id="title">Hello World</h1> // JavaScript let titleElement = document.getElementById("title"); console.log(titleElement); // <h1 id="title">Hello World</h1>
querySelector()
// Select by ID let title = document.querySelector("#title"); // Select by class let button = document.querySelector(".btn"); // Select by tag let paragraph = document.querySelector("p");
querySelectorAll()
// Select ALL elements with class "card" let cards = document.querySelectorAll(".card"); console.log(cards.length); // Number of cards
โ๏ธ Changing HTML Content
textContent
// HTML <p id="demo">Original text</p> // JavaScript let demo = document.getElementById("demo"); demo.textContent = "New text!"; // Result: <p id="demo">New text!</p>
innerHTML
// Can include HTML tags let container = document.getElementById("container"); container.innerHTML = "<h2>Hello</h2><p>Welcome!</p>";
๐จ Changing CSS Styles
let box = document.getElementById("box"); // Change individual styles box.style.color = "red"; box.style.backgroundColor = "yellow"; box.style.fontSize = "24px"; box.style.padding = "20px"; box.style.border = "2px solid black";
โ Adding/Removing Classes
let element = document.getElementById("myElement"); // Add class element.classList.add("active"); // Remove class element.classList.remove("hidden"); // Toggle class (add if not present, remove if present) element.classList.toggle("visible");
๐ Changing Attributes
let image = document.querySelector("img"); // Change src attribute image.src = "newimage.jpg"; // Change alt text image.alt = "New description"; // Add/change href let link = document.querySelector("a"); link.href = "https://google.com";
๐ป Complete Interactive Example
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>DOM Manipulation Demo</title> <style> body { font-family: Arial, sans-serif; padding: 40px; background: #f0f0f0; } .box { width: 200px; height: 200px; background: lightblue; margin: 20px; padding: 20px; border-radius: 10px; } .highlight { background: yellow; font-weight: bold; } button { padding: 10px 20px; margin: 5px; cursor: pointer; } </style> </head> <body> <h1 id="heading">DOM Manipulation Practice</h1> <p id="message">This is the original message</p> <div id="box" class="box"> <p>This is a box</p> </div> <button onclick="changeText()">Change Text</button> <button onclick="changeColor()">Change Color</button> <button onclick="toggleHighlight()">Toggle Highlight</button> <button onclick="addContent()">Add Content</button> <div id="output"></div> <script> // Change text content function changeText() { let heading = document.getElementById("heading"); heading.textContent = "Text Changed!"; let message = document.getElementById("message"); message.textContent = "The message has been updated"; } // Change CSS styles function changeColor() { let box = document.getElementById("box"); box.style.backgroundColor = "lightcoral"; box.style.color = "white"; box.style.fontSize = "20px"; } // Toggle CSS class function toggleHighlight() { let message = document.getElementById("message"); message.classList.toggle("highlight"); } // Add new HTML content function addContent() { let output = document.getElementById("output"); output.innerHTML += "<p>New paragraph added!</p>"; } </script> </body> </html>
๐งช Live Demo on This Page
Click the button below!
Original Box