Cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

Is it possible to create custom events?

Is it possible to create a custom event and catch it? I tried the code below and the event doesn't seem to get fired.

 


let event = new Event("hello");
document.body.dispatchEvent(event);
...

document.addEventListener("hello", () => {
console.log("CAUGHT EVENT");
});

Best Answer
0 Votes
2 REPLIES 2

Yes, it should be possible to use dispatchEvent. There's no body element though.

Best Answer
0 Votes

Can you provide a minimal example of custom events please?

Best Answer