Here's a fully working solution I based on this SO Answer: https://stackoverflow.com/a/49071358/79677
You can use it like this: SendBrowserAgnosticEvent(myElement, 'change')
and it will return the created Event
object
Here's the code in JavaScript
/** This allows us to dispatch browser events in old IE and newer browsers */
export var SendBrowserAgnosticEvent = function(elem, eventName) {
//Needed for IE Support: https://developer.mozilla.org/en-US/docs/Web/API/EventTarget/dispatchEvent#Browser_Compatibility
//https://stackoverflow.com/a/49071358/79677
var event;
if (typeof(Event) === 'function') {
event = new Event(eventName);
} else {
event = document.createEvent('Event');
event.initEvent(eventName, true, true);
}
elem.dispatchEvent(event);
return event;
};
and here it is in TypeScript with full typings
/** This allows us to dispatch browser events in old IE and newer browsers */
export const SendBrowserAgnosticEvent = <T extends HTMLElement | Window>(elem: T, eventName: string): Event => {
//Needed for IE Support: https://developer.mozilla.org/en-US/docs/Web/API/EventTarget/dispatchEvent#Browser_Compatibility
//https://stackoverflow.com/a/49071358/79677
let event;
if (typeof(Event) === 'function') {
event = new Event(eventName);
} else {
event = document.createEvent('Event');
event.initEvent(eventName, true, true);
}
elem.dispatchEvent(event);
return event;
};