Skip to content

Instantly share code, notes, and snippets.

@Strongground
Last active April 9, 2024 13:51
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Strongground/75af4eb2540d2ac64103221c0086b50b to your computer and use it in GitHub Desktop.
Save Strongground/75af4eb2540d2ac64103221c0086b50b to your computer and use it in GitHub Desktop.
emit() function for custom events
/**
* Based on Chris Ferdinandis emit() function as shown in his great newsletter.
* See also https://gomakethings.com/a-vanilla-js-custom-event-helper-function/
* Emit a custom event
* @param {String} type The event type
* @param {any} detail Any details to pass along with the event
* @param {Node} elem The element to emit the event on
*/
function emit (type, detail, elem = document) {
// Create a new event
let event = new CustomEvent(type, {
bubbles: true,
cancelable: true,
detail: detail
});
// Dispatch the event
return elem.dispatchEvent(event);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment