Last active
April 9, 2024 13:51
-
-
Save Strongground/75af4eb2540d2ac64103221c0086b50b to your computer and use it in GitHub Desktop.
emit() function for custom events
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* 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