Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Lära Event Flow Capturing and Bubbling | Event Objects and Event Flow
Quizzes & Challenges
Quizzes
Challenges
/
Event Handling and User Interaction in JavaScript

bookEvent Flow Capturing and Bubbling

When you interact with a web page—such as clicking a button inside a form—the browser handles your action as an event. But events do not just happen on a single element; they travel through a path in the Document Object Model (DOM). This journey is called event flow, and it occurs in two main phases: capturing and bubbling.

Imagine dropping a letter into a building's mailbox. The letter passes from the building's entrance (the highest level), through the lobby and hallways (intermediate levels), and finally reaches the specific apartment (the target element). This is similar to the capturing phase, where the event starts at the top of the DOM (the document) and travels down through each ancestor element until it reaches the one you interacted with.

Once the event reaches the target element, it does not stop there. Instead, it begins to bubble back up through the ancestors, like echoes traveling back up the building, giving each level a chance to react. This is the bubbling phase.

Here's a diagram to visualize event flow:

[Document]
   |
   v
[Body]
   |
   v
[Div]
   |
   v
[Button]  <-- You click here (target)
   ^
   |
[Div]
   ^
   |
[Body]
   ^
   |
[Document]
  • During capturing, the event moves from the top (document) down to the target element;
  • At the target, handlers registered for both phases can run;
  • During bubbling, the event moves back up from the target to the top.

In real-world terms, think of event capturing as a manager passing a message down the chain of command to a team member, and bubbling as the team member's response traveling back up to the manager. Both phases give different elements a chance to handle the event, and you can choose which phase to listen on when attaching an event listener.

script.js

script.js

index.html

index.html

style.css

style.css

copy
question mark

Which of the following best describes the order in which event handlers are triggered when you click a button inside nested elements with both capturing and bubbling listeners?

Select the correct answer

Var allt tydligt?

Hur kan vi förbättra det?

Tack för dina kommentarer!

Avsnitt 2. Kapitel 3

Fråga AI

expand

Fråga AI

ChatGPT

Fråga vad du vill eller prova någon av de föreslagna frågorna för att starta vårt samtal

Suggested prompts:

Can you explain the difference between event capturing and bubbling in more detail?

How do I choose which phase to listen to when adding an event listener?

Can you give an example of how to use event capturing and bubbling in JavaScript?

bookEvent Flow Capturing and Bubbling

Svep för att visa menyn

When you interact with a web page—such as clicking a button inside a form—the browser handles your action as an event. But events do not just happen on a single element; they travel through a path in the Document Object Model (DOM). This journey is called event flow, and it occurs in two main phases: capturing and bubbling.

Imagine dropping a letter into a building's mailbox. The letter passes from the building's entrance (the highest level), through the lobby and hallways (intermediate levels), and finally reaches the specific apartment (the target element). This is similar to the capturing phase, where the event starts at the top of the DOM (the document) and travels down through each ancestor element until it reaches the one you interacted with.

Once the event reaches the target element, it does not stop there. Instead, it begins to bubble back up through the ancestors, like echoes traveling back up the building, giving each level a chance to react. This is the bubbling phase.

Here's a diagram to visualize event flow:

[Document]
   |
   v
[Body]
   |
   v
[Div]
   |
   v
[Button]  <-- You click here (target)
   ^
   |
[Div]
   ^
   |
[Body]
   ^
   |
[Document]
  • During capturing, the event moves from the top (document) down to the target element;
  • At the target, handlers registered for both phases can run;
  • During bubbling, the event moves back up from the target to the top.

In real-world terms, think of event capturing as a manager passing a message down the chain of command to a team member, and bubbling as the team member's response traveling back up to the manager. Both phases give different elements a chance to handle the event, and you can choose which phase to listen on when attaching an event listener.

script.js

script.js

index.html

index.html

style.css

style.css

copy
question mark

Which of the following best describes the order in which event handlers are triggered when you click a button inside nested elements with both capturing and bubbling listeners?

Select the correct answer

Var allt tydligt?

Hur kan vi förbättra det?

Tack för dina kommentarer!

Avsnitt 2. Kapitel 3
some-alt