Search code examples
javascripteventscustom-events

How to trigger a custom event in capture phase


Is there a way to trigger a custom event in javascript, so that it will propagate using the capture model?

I have a hierarchy of elements that I use as a mean to communicate between objects. Each level of the hierarchy represents a sub-family of object. Ideally, I could trigger an event to each family's or sub-family's objects using their common ancestor.

eg:

animals |_ | mammals | |_cats | |_dogs |_ reptiles |_alligators |_dinosaurs

(please no comments on my inadequate taxonomy :) )

I would like to be able to trigger an event for all animals, all mammals or all cats, or event to a single cat instance.

However, I can only find examples of custom events using the bubbling phase, so I want to know if it's me missing something or is this really impossible.

I know I can use event delegation on the root element for all instance, but it will scale really badly (this system needs to handle thousands of events of all types).

Any ideas?


Solution

  • This is not possible with current DOM event specification, as it only supports the bubble phase.