Custom Events and Event Dispatching in Javascript


Create and dispatch custom events.

Source Code

let event = new CustomEvent('myEvent', { detail: { message: 'Hello' } });
document.dispatchEvent(event);
document.addEventListener('myEvent', function(e) {
  console.log(e.detail.message); // 'Hello'
});
Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments