|
| 1 | +// 📌 JavaScript Intermediate - DOM Manipulation |
| 2 | + |
| 3 | +// Welcome to the second section of the JavaScript Intermediate tutorial! |
| 4 | +// Here, you'll learn how to interact with and modify the Document Object Model (DOM). |
| 5 | + |
| 6 | +// Selecting Elements |
| 7 | +const heading = document.getElementById("main-title"); // Select by ID |
| 8 | +const paragraphs = document.getElementsByClassName("text"); // Select by class |
| 9 | +const allDivs = document.getElementsByTagName("div"); // Select by tag |
| 10 | +const firstItem = document.querySelector(".item"); // Select first match |
| 11 | +const allItems = document.querySelectorAll(".item"); // Select all matches |
| 12 | + |
| 13 | +console.log("Heading:", heading); |
| 14 | +console.log("Paragraphs:", paragraphs); |
| 15 | +console.log("All Divs:", allDivs); |
| 16 | +console.log("First Item:", firstItem); |
| 17 | +console.log("All Items:", allItems); |
| 18 | + |
| 19 | +// Modifying Elements |
| 20 | +heading.textContent = "Updated Title"; // Change text |
| 21 | +heading.style.color = "blue"; // Change style |
| 22 | +heading.classList.add("highlight"); // Add a class |
| 23 | + |
| 24 | +// Creating and Appending Elements |
| 25 | +const newParagraph = document.createElement("p"); // Create new element |
| 26 | +newParagraph.textContent = "This is a new paragraph."; |
| 27 | +document.body.appendChild(newParagraph); // Append to body |
| 28 | + |
| 29 | +// Event Listeners |
| 30 | +const button = document.getElementById("myButton"); |
| 31 | +button.addEventListener("click", function () { |
| 32 | + alert("Button clicked!"); |
| 33 | +}); |
| 34 | + |
| 35 | +// 💡 Practice selecting, modifying, and handling events in the DOM! |
0 commit comments