Event 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
index.html
style.css
Bedankt voor je feedback!
Vraag AI
Vraag AI
Vraag wat u wilt of probeer een van de voorgestelde vragen om onze chat te starten.
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?
Awesome!
Completion rate improved to 6.25
Event Flow Capturing and Bubbling
Veeg om het menu te tonen
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
index.html
style.css
Bedankt voor je feedback!