dispatchEvent not working in IE11

user4014674 picture user4014674 · Nov 27, 2014 · Viewed 45.9k times · Source

I am using the following code to submit to a form:

element.dispatchEvent(new Event("submit"));

Inspector returns the error: Object doesn't support this action

This works in Chrome.

The purpose of this command is to make a division call the submit event on a form when clicked.

Jquery is not an option

Answer

Kohei Mikami picture Kohei Mikami · Mar 2, 2018

This is the best way to make it work for IE11 and other browsers with considering future changes.

var event;
if(typeof(Event) === 'function') {
    event = new Event('submit');
}else{
    event = document.createEvent('Event');
    event.initEvent('submit', true, true);
}

$el.dispatchEvent(event);