Event Buddling and Capturing

Overview

  1. Capturing phase – the event goes down to the element.

  2. Target phase – the event reached the target element.

  3. Bubbling phase – the event bubbles up from the element.

Buddling

  • When an event happens on an element, it first runs the handlers on it, then on its parent, then all the way up on other ancestors.

<style>
  body * {
    margin: 10px;
    border: 1px solid blue;
  }
</style>

<form onclick="console.log('form')">FORM
  <div onclick="console.log('div')">DIV
    <p onclick="console.log('p')">P</p>
  </div>
</form>

Output:
p div form

Targeting

  • The most deeply nested element that caused the event is called a target element, accessible as event.target.

  • Note the differences from event.currentTarget:

    • event.target – is the “target” element that initiated the event, it doesn’t change through the bubbling process.

    • event.currentTarget – is the “current” element, the one that has a currently running handler on it.

  • In form.onclick handler:

    • event.currentTarget is the <form> element, because the handler runs on it.

    • event.target is the actual element inside the form that was clicked.

  • event.stopPropagation() stop event budding up to parent element

Capturing

  • To catch an event on the capturing phase, we need to set the handler capture option to true:

  • Example

Custom Event

Reference

Last updated

Was this helpful?