


Khusboo Tayal
The Document Object Model (DOM) is the interface between JavaScript and HTML, allowing you to dynamically access and manipulate the structure, style, and content of a web page. Understanding DOM manipulation and events is crucial for building interactive web applications.
<!DOCTYPE html> <html> <head> <title>DOM Example</title> </head> <body> <h1 id="title">Hello, World!</h1> <button id="changeTextBtn">Change Text</button> </body> </html>
JavaScript provides multiple ways to access DOM elements.
const title = document.getElementById("title");
console.log(title.textContent); // Output: Hello, World!
<p class="text">First Paragraph</p>
<p class="text">Second Paragraph</p>
const paragraphs = document.getElementsByClassName("text");
console.log(paragraphs[0].textContent); // Output: First Paragraph
const allButtons = document.getElementsByTagName("button");
console.log(allButtons.length); // Output: 1
const title = document.querySelector("#title");
console.log(title.textContent); // Output: Hello, World!const paragraphs = document.querySelectorAll(".text");
console.log(paragraphs.length); // Output: 2Once an element is selected, you can manipulate its content, style, attributes, and structure.
const title = document.getElementById("title");
title.textContent = "Hello, JavaScript!";title.innerHTML = "<span style='color: red;'>Hello, JavaScript!</span>";
title.style.color = "blue"; title.style.fontSize = "24px";
title.classList.add("highlight");
title.classList.remove("highlight");
title.classList.toggle("highlight");
const newParagraph = document.createElement("p");
newParagraph.textContent = "This is a new paragraph.";
document.body.appendChild(newParagraph);document.body.removeChild(newParagraph); // Using parent.removeChild()
const newTitle = document.createElement("h2");
newTitle.textContent = "New Title";
document.body.replaceChild(newTitle, title);An event is an action or occurrence detected by JavaScript, such as a user clicking a button, typing, or scrolling.
const button = document.getElementById("changeTextBtn");
button.addEventListener("click", function() {
title.textContent = "Text Changed!";
});
function changeText() {
title.textContent = "Text Changed!";
}
button.addEventListener("click", changeText);
// Removing the event listener
button.removeEventListener("click", changeText);<div id="parent" style="padding: 20px; background: lightgray;">
Parent
<button id="child">Click Me</button>
</div>
document.getElementById("parent").addEventListener("click", function() {
console.log("Parent Clicked");
}, true); // true means capturing
document.getElementById("child").addEventListener("click", function(event) {
console.log("Child Clicked");
event.stopPropagation(); // Stops propagation
});Instead of attaching event listeners to each child element, you can attach it to a parent and use event delegation.
<ul id="menu">
<li>Home</li>
<li>About</li>
<li>Contact</li>
</ul>
document.getElementById("menu").addEventListener("click", function(event) {
if (event.target.tagName === "LI") {
console.log("You clicked on: " + event.target.textContent);
}
});