Web Development - HTML, CSS & JavaScript: post #3808

document.getElementById("parent")
.addEventListener("click", function() {
console.log("Parent Clicked");
});

document.getElementById("child")
.addEventListener("click", function() {
console.log("Child Clicked");
});


Clicking the button produces:

Child Clicked
Parent Clicked


This happens because the event bubbles from the child to the parent.

💻 Real-World Example: Interactive Counter

Let's create a simple counter using DOM manipulation.

<h2 id="count">0</h2>

<button id="increase">Increase</button>
<button id="decrease">Decrease</button>


```javascript
let count = 0;

let display = document.getElementById("count");

document.getElementById("increase")
.addEventListener("click", function() {
count++;
display.textContent = count;
});

document.getElementById("decrease")
.addEventListener("click", function() {
count--;
display.textContent = count;
});`


This example demonstrates how JavaScript updates webpage content based on user interaction.

⚡ Quick Revision

• document → Represents the webpage.

• getElementById() → Selects an element by ID.

• querySelector() → Selects the first matching element.

• querySelectorAll() → Selects all matching elements.

• textContent → Changes or retrieves text.

• innerHTML → Changes or retrieves HTML markup.

• style → Changes CSS styles.

• classList → Manages CSS classes.

• addEventListener() → Attaches an event handler.

• createElement() → Creates an HTML element.

• appendChild() → Adds an element.

• remove() → Removes an element.

• .value → Gets an input's value.

• parentElement → Gets the parent element.

💡 Key Takeaways

• DOM connects JavaScript with HTML.

• JavaScript can dynamically modify webpage content and styling.

• Events make webpages interactive.

• DOM manipulation is essential for frontend development.

• Understanding the DOM is a foundation for learning frameworks like React.

📌 Double Tap ❤️ For More
❤2