Coder Social home page Coder Social logo

event-capture's Introduction

This repository exists to demonstrate the difference between event capturing and event bubbling.

Running

To run, simply open event_capture.html in a browser. Open the console, click the link, and observe the order of the fired events.

Explanation

A simple two-level hierarchy is created: a <div> containing a link <a>. A number of event listeners are registered on both DOM elements.

First, event listeners are registered against the <a> itself (the target to be clicked). In order, these event listeners are set to fire on: bubble, capture, bubble, and captured. Next, two event listeners are registered on the parent: in order, a bubble and a capture event handler.

The order of registering is therefore as follows:

  • target (bubble)
  • target (capture)
  • target (bubble)
  • target (capture)
  • parent (bubble)
  • parent (capture)

The actual order of output is as follows:

  • parent (capture)
  • target (bubble)
  • target (capture)
  • target (bubble)
  • target (capture)
  • parent (bubble)

This therefore demonstrates the order of event capturing/bubbling:

  • The event capture phase goes from window to (but not including) the target element, triggering any event listeners along the way that have their capture value set to true.
  • The event target phase triggers all event listeners on the target element, regardless of their capture value, in order of their registration.
  • The event bubble phase goes from the target's parent (ie. not including the target) to the window, triggering any event listeners along the way that have the capture value set to false.

This demonstrates that we can therefore take advantage of event capturing in order to trigger certain callbacks to fire before the event bubbling phase. However, as demonstrated, this only works if we listen on a parent of the actual event target. Any events listeners registered on the event target trigger in order of registration, so we cannot control their order of activation.

event-capture's People

Contributors

danielmoniz-gbi avatar

Watchers

James Cloos avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.