DEV Community

Cover image for Event Propagation: Capturing, Bubbling
uu
uu

Posted on

 

Event Propagation: Capturing, Bubbling

DOM event flow has three phases: Capture, Target, Bubbling phase.

<html>

<head>
    <title>Event Propagation</title>
    <style>
        #parent {
            width: 200px;
            height: 200px;
            text-align: center;
            line-height: 3;
            background: green;
        }

        #child {
            width: 100px;
            height: 100px;
            margin: 0 auto;
            background: orange;
        }
    </style>
</head>

<body>
    <div id="parent">
        Parent element
        <div id="child">
            Children element
        </div>
    </div>
    <script type="text/javascript">
        var parent = document.getElementById("parent");
        var child = document.getElementById("child");

        document.body.addEventListener("click", function (e) {
            console.log("click-body");
        }, false);

        parent.addEventListener("click", function (e) {
            console.log("click-parent");
        }, true);

        child.addEventListener("click", function (e) {
            console.log("click-child");
        }, false);
    </script>
</body>

</html>
Enter fullscreen mode Exit fullscreen mode

Alt Text

Event Capturing

When clicking mouse or triggering dom event(here click child's div), browser will propagate event from outside to target,starting from root(document).

See the code above, parent's listener were registered using a value of true in addEventListener

parent.addEventListener('click', listener, true)
Enter fullscreen mode Exit fullscreen mode

If third parameter is omitted, its default value is false and the listener is not a capturer. Then it is not event capturing, event bubbling instead.

Event Bubbling

Event Bubbling is other way around. Its event order is from target to outside(can be parent), going up back to root.

child.addEventListener("click", function (e) {
            console.log("click-child");
        }, false);
Enter fullscreen mode Exit fullscreen mode

Alt Text

Summary

1.Event Propagation order : Capturing -> Target -> Bubbling
2.When event reaches target itself, all the listeners registered on the event target will be invoked, regardless of event capturing(true) or event bubbling(default or false).

Top comments (1)

Collapse
 
waqarhussain profile image
Waqar Hussain

thanks for quick guide

The JavaScript Brief

1. Top 5 MERN STACK projects to improve your practical understanding

Boost your MERN Stack development skills by undertaking interesting beginner projects. These five engaging projects cover web applications and range from social media website applications to geo-social networking maps. Hone your understanding and apply modern techniques backed up by hands-on experience.

2. How To Optimize Your React App’s Performance

Learn the best optimizing techniques to make your React applications faster and more efficient. Focusing on the identification of performance bottlenecks and common pitfalls to avoid, these optimization strategies will keep your applications running smoothly even when faced with growing complexity.

3. A story of let, const, object mutation, and a bug in my code

In the pursuit of bug-free code, explore an incident involving a mix-up between const and let, making sure your custom code works effectively with third

party documentation. Discover best practices on program flow and learn about JavaScript's unpredictable aspects to ensure your core code is robust.