Skip to content

Instantly share code, notes, and snippets.

@cvan
Created February 7, 2014 18:02
Show Gist options
  • Save cvan/8868213 to your computer and use it in GitHub Desktop.
Save cvan/8868213 to your computer and use it in GitHub Desktop.
trigger custom events in JS
if (!('CustomEvent' in window)) {
// For IE 9/10 lol.
function CustomEvent(eventName, params) {
params = params || {bubbles: false, cancelable: false, detail: undefined};
var e = document.createEvent('CustomEvent');
e.initCustomEvent(eventName, params.bubbles, params.cancelable, params.detail);
return e;
}
CustomEvent.prototype = window.CustomEvent.prototype;
window.CustomEvent = CustomEvent;
}
function trigger(el, eventName, params) {
return el.dispatchEvent(new CustomEvent(eventName, params));
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment