Understanding Event Bubbling, Delegation, Propagation, and Preventing in JavaScript
In JavaScript, event handling is an important aspect of user interaction. When an event occurs, it not only affects the target element but can also propagate through the DOM in various ways.
Four key concepts:
- Event Bubbling
- Event Delegation
- Event Propagation
- Event Preventing
1. Event Bubbling
What is Event Bubbling?
Event Bubbling is the process where an event starts from the target element and then propagates up the DOM tree to its parent elements.
Example: Result: What is Event Delegation? Why use Event Delegation? Example: Benefits: What is Event Propagation? Example: Result: What is Event Preventing? Example: Result: This should provide a clearer understanding of these important JavaScript concepts and how they can help you handle events more efficiently. Visit my blog at: vunguyenit.site
When you click a inside a
click
event is not only triggered on the but also propagates up to the
id="parent">
id="child">Click me
document.getElementById("child").addEventListener("click", function () {
console.log("Button clicked!");
});
document.getElementById("parent").addEventListener("click", function () {
console.log("Div clicked!");
});
Button clicked!
Div clicked!
2. Event Delegation
Event Delegation is a technique where you attach the event listener to the parent element rather than to each individual child element.
Instead of attaching a click
event to each button, we can attach it to the parent ul
element:
id="menu">
Home
About
Contact
document.getElementById("menu").addEventListener("click", function (event) {
if (event.target.tagName === "LI") {
console.log("You clicked:", event.target.innerText);
}
});
elements, the event listener still works without needing to attach new listeners.
3. Event Propagation
Event Propagation is the process by which events travel through the DOM in two main phases:
document
down to the target element.
document
.
id="outer">
id="inner">Click Me
document.getElementById("outer").addEventListener(
"click",
function () {
console.log("Div clicked!");
},
true // Capture the event in the capturing phase
);
document.getElementById("inner").addEventListener(
"click",
function () {
console.log("Button clicked!");
}
);
If you click the button, the log order will be:
Div clicked!
Button clicked!
true
).
true
, the event would be processed first due to the default bubbling phase.
4. Event Preventing
Event Preventing is used to block the default behavior of the browser, for example:
link navigation.
href="https://google.com" id="myLink">Click me
document.getElementById("myLink").addEventListener("click", function (event) {
event.preventDefault(); // Prevent the link from redirecting
console.log("Link clicked but not redirected");
});
event.preventDefault()
blocked the default action.
5. Conclusion
Term
Definition
Event Bubbling
The event propagates from the target element up to its parent elements.
Event Delegation
Attach the event listener to the parent element to handle events for child elements.
Event Propagation
The process of an event traveling through the DOM (Capturing → Target → Bubbling).
Event Preventing
Prevent the default browser behavior using
event.preventDefault()
.