Skip to content

Instantly share code, notes, and snippets.

@Arifursdev
Last active February 24, 2022 13:38
Show Gist options
  • Save Arifursdev/9cfddc6defec9823cc0ef311766b1156 to your computer and use it in GitHub Desktop.
Save Arifursdev/9cfddc6defec9823cc0ef311766b1156 to your computer and use it in GitHub Desktop.
// https://stackoverflow.com/a/53768622/13359236
if (!Element.prototype.trigger)
{
Element.prototype.trigger = function(event)
{
var ev;
try
{
if (this.dispatchEvent && CustomEvent)
{
ev = new CustomEvent(event, {detail : event + ' fired!'});
this.dispatchEvent(ev);
}
else
{
throw "CustomEvent Not supported";
}
}
catch(e)
{
if (document.createEvent)
{
ev = document.createEvent('HTMLEvents');
ev.initEvent(event, true, true);
this.dispatchEvent(event);
}
else
{
ev = document.createEventObject();
ev.eventType = event;
this.fireEvent('on'+event.eventType, event);
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment