In JavaScript, event bubbling and event capturing are very interesting topics that relate to the propagation of the JavaScript event-listener
from parent to child and vice versa.
In JavaScript, an event-listener
is a function that waits for the occurrence of a particular event.
To add event-listener
to any element, we use the addEventListener()
function.
This function accepts the following parameters:
eventName
: This is the name of the event. For example, click, mouseup, and so on.callback
: This is the function to be invoked when the event occurs.{capture: boolean}
: This is a boolean that specifies whether the mode of event propagation should be bubbling or capturing. The default value of capture is false
, and therefore the default mode is event bubbling. To set the mode as event capturing, we need to set the value of capture as true
.The procedure of event propagation from the closest element to the farthest element is called event bubbling.
Whenever an event occurs on an element, the event handlers first run on it, and then that event propagates to its parent element, then to its grandparent element, and so on.
The procedure of event propagation from the farthest element to the closest element is called event capturing.
The propagation of event handlers is in the reverse direction of event bubbling. The event handlers first run on the grandparent element, then on the parent element, and finally on the child element.
Let's see a few examples of event bubbling and event capturing in the following code snippets:
Let's look at the code below:
In the HTML tab:
id="grandparent"
.id="parent"
.id="child"
.In the JavaScript tab:
grandParent
, parent
, and child
elements present in the DOM using the getElementById()
function.click
event listener to the grandparent
element.click
event listener to the parent
element.click
event listener to the child
element.In the CSS tab:
When the inner-most square (with yellow background) is clicked, the event handler is called on it first and then propagates to the next square (with blue background) and finally to the last square (with red background).
As the event propagates, the callback function of each listener is executed and the respective statement is printed on the console.
Let's look at the code below:
In the HTML tab:
id="grandparent"
.id="parent"
.id="child"
.In the JavaScript tab:
grandParent
, parent
, and child
elements present in the DOM using the getElementById()
function.click
event listener to the grandparent
element.click
event listener to the parent
element.click
event listener to the child
element.Note: We have added
{capture: true}
on each of the event listeners. This sets the mode of propagation as event capturing.
In the CSS tab:
When the inner-most square (with yellow background) is clicked, the event handler is called on the last square (with red background) and then propagates to the child square (with blue background) and finally to itself.
As the event propagates, the callback function of each listener is executed and the respective statement is printed to the console.