Home > Web Front-end > JS Tutorial > Mastering Event Bubbling and Capturing in JavaScript

Mastering Event Bubbling and Capturing in JavaScript

Barbara Streisand
Release: 2024-12-31 06:55:20
Original
489 people have browsed it

Mastering Event Bubbling and Capturing in JavaScript

Event Bubbling and Capturing in JavaScript

Event propagation in JavaScript describes the way events flow through the DOM after being triggered. There are two main phases: Event Bubbling and Event Capturing. Understanding these concepts is crucial for managing event listeners effectively.


1. Event Propagation Phases

When an event is triggered, it propagates through the DOM in the following order:

  1. Capturing Phase: The event travels from the root of the DOM tree down to the target element.
  2. Target Phase: The event reaches the target element.
  3. Bubbling Phase: The event travels from the target element back up to the root.

2. Event Bubbling

In the bubbling phase, the event starts at the target element and bubbles up through its ancestors.

Example:

<div>





<pre class="brush:php;toolbar:false">document.getElementById("parent").addEventListener("click", function() {
  console.log("Parent clicked");
});

document.getElementById("child").addEventListener("click", function() {
  console.log("Child clicked");
});
Copy after login
Copy after login

Output when clicking the button:

Child clicked
Parent clicked
Copy after login
Copy after login
  • The event bubbles up from the button to the div.

Stopping Bubbling

Use the stopPropagation() method to prevent the event from propagating further.

document.getElementById("child").addEventListener("click", function(event) {
  event.stopPropagation();
  console.log("Child clicked");
});
Copy after login
Copy after login

3. Event Capturing (Trickling)

In the capturing phase, the event travels from the root of the DOM tree down to the target element.

Example:

document.getElementById("parent").addEventListener(
  "click",
  function() {
    console.log("Parent clicked");
  },
  true // Enables capturing phase
);

document.getElementById("child").addEventListener("click", function() {
  console.log("Child clicked");
});
Copy after login

Output when clicking the button:

Parent clicked
Child clicked
Copy after login
  • The event is captured by the parent before reaching the child.

4. Comparing Bubbling and Capturing

Feature Event Bubbling Event Capturing
Feature Event Bubbling Event Capturing
Order From target to ancestors From root to target
Default Behavior Enabled Disabled unless specified
Use Case Commonly used for delegation Less commonly used
Order
From target to ancestors From root to target
Default Behavior Enabled Disabled unless specified
Use Case Commonly used for delegation Less commonly used

5. Event Delegation

Event delegation takes advantage of event bubbling to efficiently handle events for multiple child elements.

Example:

<div>





<pre class="brush:php;toolbar:false">document.getElementById("parent").addEventListener("click", function() {
  console.log("Parent clicked");
});

document.getElementById("child").addEventListener("click", function() {
  console.log("Child clicked");
});
Copy after login
Copy after login
  • A single event listener on the ul handles clicks for all li elements.

6. Preventing Default Behavior

Use the preventDefault() method to stop the default behavior of an element without affecting propagation.

Example:

Child clicked
Parent clicked
Copy after login
Copy after login

7. Practical Use Case: Form Validation

document.getElementById("child").addEventListener("click", function(event) {
  event.stopPropagation();
  console.log("Child clicked");
});
Copy after login
Copy after login

8. Summary

  • Event bubbling propagates events from the target element upward to ancestors.
  • Event capturing propagates events from the root downward to the target element.
  • Use stopPropagation() to halt propagation and preventDefault() to cancel default actions.
  • Event delegation is a powerful technique for managing events efficiently on dynamic elements.

Mastering event bubbling and capturing ensures better control over event-driven applications and improves code efficiency.

Hi, I'm Abhay Singh Kathayat!
I am a full-stack developer with expertise in both front-end and back-end technologies. I work with a variety of programming languages and frameworks to build efficient, scalable, and user-friendly applications.
Feel free to reach out to me at my business email: kaashshorts28@gmail.com.

The above is the detailed content of Mastering Event Bubbling and Capturing in JavaScript. For more information, please follow other related articles on the PHP Chinese website!

source:dev.to
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template