Event Bubbling
Event bubbling describes how the browser handles events targeted at nested elements.
When you click on a button, you are inadvertently clicking on things that inside the button, as well as things that contain the button
- ex. when you click on a
<strong>tag, the event happens on that tag. But if nothing happens with that event, then it will bubble up to the surrounding element (ex. a Button). If the button wasn't listening for that specific event, then it will keep bubbling up onto the next surrounding element. - This is necessary, because if we have a
<strong>tag inside a<button>and we click on that<strong>(which doesn't have a click handler), then our Button needs some way of being notified of that click event. event.targetis a reference to the object onto which the event was dispatched.- ex. if we click a button, then
event.targetis the<button>tag.
- ex. if we click a button, then
Event capturing is the process of figuring out what got clicked, and it happens after the bubbling phase
- ex. when you click on a
span, the browser starts at the top: "user clicked on thewindow>html>body>div>button>span"
When we add an event listener, we can specify that it triggers on the capture phase or bubble phase
- bubble phase is default, and is what we want 99% of the time.
currentTarget vs target
currentTarget
- what actually got clicked, prior to any bubbling happening
- ex. we have a
spaninside abutton. If the user clicks thespan, thenspanis thecurrentTargetthat got clicked, and thebuttonis thetarget
target
- what you listened for a click on (what the bubbling ended up at)
UE Resources
Backlinks