Event Capturing And Bubbling In JavaScript

Introduction

 
The DOM has two approaches for an object to sense events: the first one is top-down approach and the second is the bottom-up approach. The top-down approach is called event capture whereas the bottom-up is known as event bubbling.
 
Let’s start with example where you have one HTML which contains <div>, <p> under <div>, and <input> under <p>.
 
By doing this, you will have the below ready code.
  1. <html>    
  2.     <head>    
  3.     </head>    
  4.     <body>    
  5.         <div id='divMain'>    
  6.             <p id='pMain'>    
  7.                 <input id='btnClick' type='button' value='Click!'/>    
  8.             </p>    
  9.         </div>    
  10.     </body>    
  11. </html>  

Event Capturing

 
To understand what is event capturing, add an event listener to document, div, p, and input. Also, create functions for each listener. By doing this, you will have the below code.
  1. <html>    
  2.     <head>    
  3.     </head>    
  4.     <body>    
  5.         <div id='divMain'>    
  6.             <p id='pMain'>    
  7.                 <input id='btnClick' type='button' value='Click!'/>    
  8.             </p>    
  9.         </div>    
  10.         <script>    
  11.             document.addEventListener('click', documentClick, true);    
  12.             document.getElementById('btnClick').addEventListener('click', btnClick, true);    
  13.             document.getElementById('pMain').addEventListener('click', pMainClick, true);    
  14.             document.getElementById('divMain').addEventListener('click', divMainClick, true);    
  15.     
  16.             function documentClick(){    
  17.                 console.log('Document clicked');                    
  18.             }    
  19.             function btnClick(){    
  20.                 console.log('Button clicked');    
  21.             }    
  22.             function pMainClick(){    
  23.                 console.log('Main paragraph clicked');    
  24.             }    
  25.             function divMainClick(){    
  26.                 console.log('Main div clicked');    
  27.             }    
  28.         </script>    
  29.     </body>    
  30. </html>   
The addEventListener() function has two mandatory parameters, “event” and “function” and one optional parameter, “useCapture”. userCapture has two possible values “true” and “false”. If “true” is set, then the event handler will execute in the capturing phase. 
 
So, as written in the code, if we click on button, the output will be as below.
 
JS
 
In this case, even though we are clicking the button, it is calling the document first and button at last. As written in the introduction, Event Capturing is a Top-Down approach. This is why it is calling the click event of the document first and button at last in the sequence.
 
JS
 

Event Bubbling

 
Event bubbling is a bottom-up approach, so the sequence of calling events is as below.
 
JS
 
In this case, click event of the button will be called first and the document at last in sequence. To achieve this, we are using the same example as event capturing. The code for it will be as below.
  1. <html>    
  2.     <head>    
  3.     </head>    
  4.     <body>    
  5.         <div id='divMain'>    
  6.             <p id='pMain'>    
  7.                 <input id='btnClick' type='button' value='Click!'/>    
  8.             </p>    
  9.         </div>    
  10.         <script>    
  11.             document.addEventListener('click', documentClick, false);    
  12.             document.getElementById('btnClick').addEventListener('click', btnClick, false);    
  13.             document.getElementById('pMain').addEventListener('click', pMainClick, false);    
  14.             document.getElementById('divMain').addEventListener('click', divMainClick, false);    
  15.     
  16.             function documentClick(){    
  17.                 console.log('Document clicked');                    
  18.             }    
  19.             function btnClick(){    
  20.                 console.log('Button clicked');    
  21.             }    
  22.             function pMainClick(){    
  23.                 console.log('Main paragraph clicked');    
  24.             }    
  25.             function divMainClick(){    
  26.                 console.log('Main div clicked');    
  27.             }    
  28.         </script>    
  29.     </body>    
  30. </html>    
Here, we are passing “false” as “userCapture” parameter in addEventListener(). This will allow you to change the event calling flow in bottom-up approach.
 
Output for the above code will be as follows.
 
Output