JavaScript DOM Manipulation 🌐

JavaScript DOM Manipulation 🌐
The Document Object Model (DOM) allows JavaScript to interact with and modify an HTML document dynamically.
1️⃣ What is the DOM?
The DOM represents an HTML page as a tree-like structure, where:
HTML elements are nodes.
JavaScript can select, modify, and delete elements.
Example HTML Document:
<!DOCTYPE html>
<html lang="en">
<head>
<title>DOM Example</title>
</head>
<body>
<h1 id="title">Hello, World!</h1>
<p class="info">Welcome to JavaScript DOM manipulation.</p>
<button id="btn">Click Me</button<
</body>
</html>
let title = document.getElementById("title");
console.log(title); //Hello, World!
let paragraphs = document.getElementsByClassName("info");
console.log(paragraphs[0]); //Welcome to JavaScript DOM manipulation.
let firstParagraph = document.querySelector(".info");
console.log(firstParagraph); //Welcome to JavaScript DOM manipulation.
let allParagraphs = document.querySelectorAll(".info");
console.log(allParagraphs); // NodeList of matching elements
title.textContent = "Welcome to the DOM!";
firstParagraph.innerHTML = "<strong>DOM is powerful!</strong>";
button.addEventListener("click", function() {
title.textContent = "You clicked the button!";
});
Date: 2025-03-29 00:00:00.000000