68

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

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129

4 Answers4

99

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);
seebiscuit
  • 4,905
  • 5
  • 31
  • 47
Kohei Mikami
  • 2,850
  • 24
  • 21
  • 9
    By default the Event does not bubble and is not cancelable - see https://developer.mozilla.org/en-US/docs/Web/API/Event/Event. To get the same behaviour in both cases initiEvent('submit', false, false) should be called. – Philip May 02 '19 at 19:04
  • 1
    Doesn't work in Edge in the case of DragEvent https://developer.microsoft.com/en-us/microsoft-edge/platform/issues/14156632/ – Andrey Stukalin Sep 27 '19 at 06:16
  • Great cross browser solution. – Y.K. May 10 '20 at 14:24
48

I just had the same problem, but the following seems to work in IE11:

var event = document.createEvent("Event");
event.initEvent("submit", false, true); 
// args: string type, boolean bubbles, boolean cancelable
element.dispatchEvent(event);
Taimo Kolsar
  • 489
  • 4
  • 4
  • 1
    No, don't use this. it's deprecated according to https://developer.mozilla.org/en-US/docs/Web/API/Event/initEvent – Gaui Jan 04 '16 at 10:19
  • 11
    You MUST use this if you want it to work on IE, see https://developer.mozilla.org/en-US/docs/Web/Guide/Events/Creating_and_triggering_events#Creating_custom_events and https://msdn.microsoft.com/en-us/library/ff975247(v=vs.85).aspx This is, currently, may be MS update their software. – fbiazi Feb 26 '17 at 02:47
  • Yeah you gotta use this if you want it to be compatible with IE11 unfortunately. – yoyodunno May 26 '17 at 15:04
  • 4
    For `CustomEvent` at least, a more future-proof solution is to use a polyfill. This may not be applicable to the `Event` constructor though. See https://stackoverflow.com/questions/26596123/internet-explorer-9-10-11-event-constructor-doesnt-work, https://developer.mozilla.org/en-US/docs/Web/API/CustomEvent/CustomEvent The poly fill fixed the problem for me working with https://developer.mozilla.org/en-US/docs/Web/API/CustomEvent in IE 11 – Nathan Jun 12 '17 at 20:50
  • 3
    Also note that IE will not dispatch from disabled elements, contrary to Chrome. – Sjeiti May 15 '18 at 08:57
  • bubbles false was for me the working input for the initEvent() – Emre Öztürk Oct 07 '19 at 11:10
  • bubbles false WAS working for me a few weeks ago. Updated to react 17 (coincidence?) and had to change to bubbles true. Also not sure of the difference between "Event" and "HtmlEvents", coz both work for me (at the moment) – Jarrod McGuire Feb 02 '21 at 15:06
14

It's best to use a polyfil to fix this. (custom-event-polyfill)

# using yarn 
$ yarn add custom-event-polyfill

# using npm 
$ npm install --save custom-event-polyfill

then include/require it in your javascript

import 'custom-event-polyfill';

https://www.npmjs.com/package/custom-event-polyfill

Sanath
  • 4,774
  • 10
  • 51
  • 81
2

I assembled bits and pieces of various approaches and got this to work:

var customEvent = document.createEvent('HTMLEvents');
customEvent.initEvent('myCustomEvent', true, true);
document.dispatchEvent(customEvent);

To be honest, this doesn't make a lot of sense to me. It creates an event (naming it HTMLEvents seems to be required) on the document, then goes and initializes that event with another name. If anyone can explain this better please add a comment below so it can be incorporated into the answer.

In any case, I'm able to listen to this custom event in IE11 (and modern browsers) with a standard event listener:

document.addEventListener('myCustomEvent', function(){
  console.log('Event received.');
});
Mike Wheaton
  • 492
  • 4
  • 15
  • Hi Mike, 'HTMLEvents' is not the name, it's the _category_ of custom event you're creating. And 'HTMLEvents' doesn't even seem to be a valid value, which further makes things strange. ;-) The actual event you're creating is called `myCustomEvent` (could also be `change`, `click` or other events) and you're storing it in the variable `customEvent`. (And I agree, this is a very weird interface.) More info here: https://msdn.microsoft.com/en-us/windows/ff975304(v=vs.71) – Eric Nov 08 '19 at 12:14
  • Late answer, but 'HTMLEvents' it's a **valid** value Eric, you can read about it on the same link you shared, it specifies that 'HTMLEvents' is indeed a type of event. You can read further about it here: https://dom.spec.whatwg.org/#dom-document-createevent The code I had was exactly like the first one, except that the type was simply 'Event'. This worked on Chrome and Firefox, but not on Edge. With 'HTMLEvents' I made it work too with Edge! Thanks for the tip Mike :) – Gummiees Oct 06 '20 at 07:34