• Home
  • Categories
  • Video Tutorials
    • Angular 5
  • News
  • About us
  • Contact us
  • Login
test
Code4Developers

Code4Developers

Code4Developers
  • Home
  • Categories
  • Video Tutorials
    • Angular 5
  • News
  • About us
  • Contact us
  • Login
  • JavaScript

Event Capturing and Bubbling in JavaScript

  • Yatendrasinh Joddha
  • July 21, 2017
  • 2 minute read
javascript
Total
0
Shares
0
0
0

The DOM has two approaches for object to sense events: first is top down approach and second is bottom up approach. Top down approach is called event capturing whereas 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 below ready code

<html>
    <head>
    </head>
    <body>
        <div id='divMain'>
            <p id='pMain'>
                <input id='btnClick' type='button' value='Click!'/>
            </p>
        </div>
    </body>
</html>

Event Capturing

To understand what is event capturing add event listener to document, div, p, and input, also create functions for each listener. By doing this you will have below code

<html>
    <head>
    </head>
    <body>
        <div id='divMain'>
            <p id='pMain'>
                <input id='btnClick' type='button' value='Click!'/>
            </p>
        </div>
        <script>
            document.addEventListener('click', documentClick, true);
            document.getElementById('btnClick').addEventListener('click', btnClick, true);
            document.getElementById('pMain').addEventListener('click', pMainClick, true);
            document.getElementById('divMain').addEventListener('click', divMainClick, true);

            function documentClick(){
                console.log('Document clicked');               
            }

            function btnClick(){
                console.log('Button clicked');
            }

            function pMainClick(){
                console.log('Main paragraph clicked');
            }

            function divMainClick(){
                console.log('Main div clicked');
            }
        </script>
    </body>
</html>

addEventListener() function have two mandatory parameters “event” and “function” and one optional parameter “useCapture”.

useCapture has two possible values “true” and “false”. If “true” is set, then the event handler will execute in the capturing phase

So as written code if we click on button then output will be as below

ECOutput

In this case even though we are clicking button, it is calling document first and button at last. As written in introduction Event Capturing is like Top Down approach, that is why it is calling click event of document first and button at last in sequence.

Event Capturing

Event Bubbling

Event bubbling is bottom up approach, so sequence of calling event is as below

Event Bubbling

In this case, click event of button will be called first and document at last in sequence. In addEventListener() function if “false” is set for useCapture parameter, then the event handler will execute in the bubbling phase.

To achieve this, we are using same example as event capturing, and code for it will be as below.

<html>
    <head>
    </head>
    <body>
        <div id='divMain'>
            <p id='pMain'>
                <input id='btnClick' type='button' value='Click!'/>
            </p>
        </div>

        <script>
            document.addEventListener('click', documentClick, false);
            document.getElementById('btnClick').addEventListener('click', btnClick, false);
            document.getElementById('pMain').addEventListener('click', pMainClick, false);
            document.getElementById('divMain').addEventListener('click', divMainClick, false);

            function documentClick(){
                console.log('Document clicked');               
            }

            function btnClick(){
                console.log('Button clicked');
            }

            function pMainClick(){
                console.log('Main paragraph clicked');
            }

            function divMainClick(){
                console.log('Main div clicked');
            }
        </script>
    </body>
</html>

Here we are passing “false” as “useCapture” parameter in addEventListener(), this will allow you change event calling flow in bottom up approach.

Output for above code will be

EBOutput

 

CLICK HERE to read more articles on JavaScript

Yatendrasinh Joddha
Yatendrasinh Joddha

I have healthy knowledge and experience in Azure, O365 and ASP.Net with c#, MVC, WebApi along with client-side frameworks and libraries like JavaScript, JQuery, KnockoutJs, AngularJs, Angular, ReactJs, NodeJs

Views: 6,066

Share this:

  • Click to share on Facebook (Opens in new window) Facebook
  • Click to share on WhatsApp (Opens in new window) WhatsApp
  • Click to share on LinkedIn (Opens in new window) LinkedIn
  • Click to share on X (Opens in new window) X
  • Click to share on Pinterest (Opens in new window) Pinterest
  • Click to email a link to a friend (Opens in new window) Email
  • Click to print (Opens in new window) Print

Like this:

Like Loading...

Related Posts

Total
0
Shares
Share 0
Tweet 0
Pin it 0
3 comments
  1. ankit says:
    July 25, 2017 at 7:02 pm

    Good one..

    Reply
  2. Pingback: Event Delegation in JavaScript | Yatendrasinh Joddha | Code4Developers
  3. Pingback: Event Delegation in JavaScript | Yatendrasinh Joddha | Code4Developers

Leave a ReplyCancel reply

Subscribe to Website via Email

Enter your email address to subscribe to this blog and receive notifications of new posts by email.

Recent Posts
  • Getting Started with AWS Step Functions: Orchestration Made Simple

    Getting Started with AWS Step Functions: Orchestration Made Simple

    2 months ago
  • React Hooks Guide: Top Tips for Optimizing Performance in Your React Applications

    React Hooks Guide: Top Tips for Optimizing Performance in Your React Applications

    2 years ago
  • Demystifying JavaScript Tree Shaking: Boosting Performance and Reducing Bundle Size

    Demystifying JavaScript Tree Shaking: Boosting Performance and Reducing Bundle Size

    2 years ago
  • Unlocking the Power of React Hooks: A Comprehensive Guide with Examples

    Unlocking the Power of React Hooks: A Comprehensive Guide with Examples

    2 years ago
  • Celebrating a Decade of Phenomenal Growth: Insights and Reflections on 10 Years of Software Engineering

    Celebrating a Decade of Phenomenal Growth: Insights and Reflections on 10 Years of Software Engineering

    2 years ago
  • Angular Custom Elements: Creating Reusable Components with Angular

    Angular Custom Elements: Creating Reusable Components with Angular

    3 years ago
  • Connect Firebase Realtime NoSQL Database with Angular App from Scratch

    Connect Firebase Realtime NoSQL Database with Angular App from Scratch

    5 years ago
  • How to Build an Inclusive Esports Community

    How to Build an Inclusive Esports Community

    5 years ago
  • Best Digital Icebreakers

    Best Digital Icebreakers

    5 years ago
  • Email alerts when a docker container stopped in AWS ECS CLUSTER

    Email alerts when a docker container stopped in AWS ECS CLUSTER

    5 years ago
Subscribe to Website via Email

Enter your email address to subscribe to this website and receive notifications of new posts by email.

Featured Posts
  • javascript 1
    Spread syntax (three dots) in JavaScript
    • March 21, 2018
  • Angular 2
    Angular 6 CRUD – Part 1: Project Setup, Routing, Service
    • May 9, 2018
  • javascript 3
    Local Storage and Session Storage
    • May 22, 2017
  • Angular 4
    Angular 4 Project Structure
    • June 18, 2017
  • AWS 5
    Email alerts when a docker container stopped in AWS ECS CLUSTER
    • July 24, 2020
Code4Developers
Learning is never ending process

Input your search keywords and press Enter.

 

Loading Comments...
 

    %d