温馨提示:本站仅提供公开网络链接索引服务,不存储、不篡改任何第三方内容,所有内容版权归原作者所有
AI智能索引来源:http://www.muo.com/javascript-event-propagation-bubbling-capturing/
点击访问原文链接

Understanding Event Propagation in JavaScript

Understanding Event Propagation in JavaScript Menu Sign in now Close Trending Submenu Productivity Android Smart TVs Networking Windows 11 Entertainment PC & Mobile Submenu Windows2 Linux Android Apple Technical Submenu Tech Explained Security Software Submenu Productivity Internet Creative Screen Submenu Entertainment Streaming Home Submenu Smart Home Home News Sign in Newsletter Menu Follow Followed Like More Action Sign in now Productivity Android Smart TVs Networking Windows 11 Entertainment Close
Understanding Event Propagation in JavaScript Source: Unsplash.com (https://unsplash.com/photos/zjq0I3XupiI) Follow Followed Like Link copied to clipboard Add us on By  Unnati Bamania Published Dec 16, 2021, 6:00 AM EST Unnati is an enthusiastic full stack developer. She loves to build projects using various programming languages. In her free time, she loves to play the guitar and is a cooking enthusiast. Sign in to your MakeUseOf account

Events in JavaScript act like notifications that a user interaction, or other action, has taken place. For instance, when you click on a form button, your browser generates an event to indicate this has happened. Typing in a search box also raises events and this is how auto-suggest often works online.

In JavaScript, events that involve user interaction usually fire against specific elements. For example, clicking a button raises an event against that button. But events also propagate: they fire against other elements in the document hierarchy.

Read on to learn all about event propagation and the two distinct types available.

What Is Event Handling in JavaScript?

You can use JavaScript code to catch, and respond to, events that a web page raises. You can do this to override default behavior or take action when no default exists. A common example is form validation. Event handling allows you to interrupt the normal process of form submission.

Consider this example:

Click Me


The above code has a button element with an id named button. It has a click event listener that displays an alert with the message Hello World.

What Is Event Propagation?

Event propagation describes the order in which the events travel through the DOM tree when the web browser fires them.

Assume there is a form tag inside a div tag, and both have onclick event listeners. Event Propagation describes how one event listener may fire after the other.

There are two types of propagation:

Event bubbling, by which events bubble upwards, from child to parent. Event capturing, by which events travel downwards, from parent to child. What Is Event Bubbling in JavaScript?

Event bubbling means the direction of event propagation will be from the child element to its parent element.

Consider the following example. It has three nested elements: div, form, and button. Each element has a click event listener. The browser displays an alert with a message when you click on each element.

By default, the order in which the web browser displays alerts will be button, form, then div. The events bubble up from child to parent.






Event propagation example



div

form 

Button
author's own screenshot What Is Event Capturing?

With event capturing, the order of propagation is the opposite of bubbling. Otherwise, the concept is identical. The only difference with capturing is that events occur from parent to child. In contrast to the previous example, with event capturing, the browser will display alerts in this order: div, form, and button.

To achieve event capturing, you need to make just one change to the code. The third parameter of addEventListener() defines the type of propagation. It is false by default, to represent bubbling. To enable event capturing, you need to set the second parameter to true.

 div.addEventListener("click", ()=>{
alert("div")
}, true);
form.addEventListener("click", ()=>{
alert("form")
}, true);
button.addEventListener("click", ()=>{
alert("button")
}, true);
How Can You Prevent Event Propagation?

You can stop the propagation of events using the stopPropagation() method. The addEventListener() method accepts an event name and a handler function. The handler takes an event object as its parameter. This object holds all the information about the event.

When you invoke the stopPropagation() method, the event will stop propagating from that point. For example, when you use stopPropagation() on the form element, events will stop bubbling up to the div. If you click the button, you'll see events raised on the button and form, but not the div.

form.addEventListener("click", (e)=>{
e.stopPropagation();
alert("form");
});

Related: The Ultimate JavaScript Cheat Sheet

JavaScript Has a Lot of Power Under the Hood

JavaScript's event handling is powerful, comparable to many full-blown interface languages. When you're developing interactive web applications, it's a vital part of your toolbox. But there are many other advanced topics to grasp. For professional JavaScript developers, there's a lot to learn!

If you want to learn to code JavaScript like a pro check out our guide to clever-one liners and prepare to wow in your next interview.

Close
Recommended 11 JavaScript One-Liners You Should Know I replaced Windows Search with a free tool and my files are now instant to find I built a $5 Wi-Fi alarm that tells me the moment my internet goes down I stopped building pivot tables for summaries after I found this new Excel function Join Our Team Our Audience About Us Press & Events Media Coverage Contact Us Follow Us Advertising Careers Terms Privacy Policies MakeUseOf is part of the Valnet Publishing Group Copyright © 2026 Valnet Inc.

Understanding Event Propagation in JavaScript,AI智能索引,全网链接索引,智能导航,网页索引

    Events are a powerful JavaScript feature. Understanding how a web browser raises them against elements is key to mastering their use.