const input = document.querySelector("input");
input.addEventListener("keyup", () => {
console.log("Typing...");
});🖱️ 13. Mouse Events
Common mouse events: click, dblclick, mouseover, mouseout, mousemove
Example:
button.addEventListener("mouseover", () => {
console.log("Mouse entered");
});🚫 14. Prevent Default Behavior
Useful for forms and links.
form.addEventListener("submit", (event) => {
event.preventDefault();
console.log("Form Submitted");
});🎯 15. Event Bubbling
When an event occurs, it moves from the child element to its parent.
Example:
parent.addEventListener("click", () => {
console.log("Parent");
});
child.addEventListener("click", () => {
console.log("Child");
});Clicking the child prints:
Child
Parent
⚡ 16. Event Delegation
Instead of adding listeners to every child element, attach one listener to the parent.
document.getElementById("list")
.addEventListener("click", (event) => {
if(event.target.tagName === "LI") {
console.log(event.target.textContent);
}
});Benefits
Better performance, Less memory usage, Works for dynamically created elements
🛠️ 17. Mini Project Example
Button Click Counter
let count = 0;
const button = document.querySelector("button");
button.addEventListener("click", () => {
count++;
button.textContent = count;
});
Every click increases the displayed count.
⭐ Most Important Interview Topics
DOM Basics, querySelector(), textContent vs innerHTML, classList, Event Listeners, Event Bubbling, Event Delegation, event.target, preventDefault()
📝 Practice Questions
Easy
Change heading text, Change background color, Hide and show a button
Medium
Build a counter, Create a to-do list, Toggle dark mode
Advanced
Form validation, Image slider, Dynamic table, Infinite scrolling
Double Tap ❤️ For More