All events that take place in the DOM are based on the Event
interface which we can also use to create our own custom event.
To do so use the Event
constructor that accepts two parameters the type
and options
.
-
type: accepts a string which acts as the name of the event
-
options: accepts an object that can have three properties
bubbles
,cancelable
, andcomposed
.- bubbles: A boolean value indicating whether the event bubbles. The default is
false
- cancelable: A boolean value indicating whether the event can be cancelled. The default is
false
- composed: A boolean value indicating whether the event will trigger listeners outside the shadow root. The default is
false
- bubbles: A boolean value indicating whether the event bubbles. The default is
Adding custom data using CustomEvent
Most of the time we would like to add more information to the custom event, that’s when the CustomEvent
interface is for.
CustomEvent is an interfaced based from the main Event interface, which also accepts the same parameters but also have the detail property in the options parameter where we can add more information.