From the course: JavaScript: Enhancing the DOM
Unlock this course with a free trial
Join today to access over 25,300 courses taught by industry experts.
Event propagation: Capturing and bubbling
From the course: JavaScript: Enhancing the DOM
Event propagation: Capturing and bubbling
- [Instructor] Let's talk about event propagation, which happens in two ways, capturing and bubbling. In order to explain this well, I want to take you through a code example. Here we have a simple button. It has a click event, and it prints "button clicked." There's a div around a button. Let's change your code a bit so that instead whenever the div gets clicked, it logs div clicked. As you can see, the div is the inner container. So I'm going to change this to inner container, and here I'm saying div clicked. Now when I click on the button, it's saying div clicked, why? Well, the button is in the div, so the button is part of the div, so indeed the div was clicked. Alright, so far so good I suppose. But what if both of them had a click event listener? Let's modify our code again. So now both the button and the div have a click event listener. And when I click it, as you can see, it works inside out. First it logs that the button was clicked, and then it works its way out to the…
Contents
-
-
-
-
-
-
-
-
(Locked)
Event listeners, event handlers, and event objects6m 26s
-
(Locked)
Common DOM events3m 38s
-
(Locked)
Removing event handlers2m 22s
-
(Locked)
Event propagation: Capturing and bubbling4m 49s
-
(Locked)
Event delegation1m 39s
-
(Locked)
Working with preventDefault2m 30s
-
(Locked)
Challenge: Eventful app53s
-
(Locked)
Solution: Eventful app1m
-
(Locked)
-
-
-
-