1

I need to trigger Enter Key Press event in IE9 using JS. Just like the below code that works for Chrome:

var evsat = document.createEvent('Event');
evsat.initEvent('keypress');
evsat.keyCode = 13;
elem.dispatchEvent(evsat);

createEvent('Event') is not supported in IE, throws:

"Object doesn't support property or method 'createEvent'"

Please suggest me some other workaround.

UPDATE: How to handle ENTER event in the code below?

https://developer.mozilla.org/en-US/docs/Web/API/CustomEvent/CustomEvent

(function() {
  if (typeof window.CustomEvent === "function") return false; /*If not IE */
  function CustomEvent(event, params) {
    params = params || {
      bubbles: false,
      cancelable: false,
      detail: undefined
    };
    var evt = document.createEvent('CustomEvent');
    evt.initCustomEvent(event, params.bubbles, params.cancelable, params.detail);
    return evt;
  }
  CustomEvent.prototype = window.Event.prototype;
  window.CustomEvent = CustomEvent;
})();
Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
Ray
  • 65
  • 1
  • 9
  • Really not hard to find duplicates searching for createEvent IE - I added the second since it has a correction to the polyfill in the first – mplungjan Jun 12 '18 at 13:32
  • @mplungjan Share the original question link – Ray Jun 12 '18 at 13:41
  • Look at the top of this page (reload if needed) Two links – mplungjan Jun 12 '18 at 13:42
  • @mplungjan I'm actually new to this. Could you modify this code according to the enter key event? Thanks! `(function () { if ( typeof window.CustomEvent === "function" ) return false; //If not IE function CustomEvent ( event, params ) { params = params || { bubbles: false, cancelable: false, detail: undefined }; var evt = document.createEvent( 'CustomEvent' ); evt.initCustomEvent( event, params.bubbles, params.cancelable, params.detail ); return evt; } CustomEvent.prototype = window.Event.prototype; window.CustomEvent = CustomEvent; })();` – Ray Jun 13 '18 at 05:08
  • I added your code to your question. Here are the relevant links I found https://stackoverflow.com/questions/26596123/internet-explorer-9-10-11-event-constructor-doesnt-work https://stackoverflow.com/questions/28815845/mouseevent-not-working-in-internet-explorer – mplungjan Jun 13 '18 at 05:24
  • @mplungjan Since the question is valid now and not duplicate, can you undo the downvote for this question? So that other may find to solve this? – Ray Jun 13 '18 at 05:58
  • Not my vote...... – mplungjan Jun 13 '18 at 06:05

0 Answers0