0

I am trying to trigger a series of events on an element in Internet Explorer but the following code gives me an "Event is undefined". any ideas what can be done to address this? createEvent is not supported by the object so cannot be used.

function categoryClick() {
  var events = ["mousemove", "mouseover", "focus", "mousedown", "mouseup", "click"];
  var combobox = document.getElementById("category");
  var a = combobox.childNodes;
  var target = a[0].childNodes[0];

    for (var i = 0; i < events.length; i++) {
    var eventObject = new Event(events[i], {
      "bubbles": true,
      "cancelable": false
    });

    target.dispatchEvent(eventObject);
  }
}
MPorter
  • 53
  • 1
  • 7
  • Possible duplicate of [Internet Explorer 9, 10 & 11 Event constructor doesn't work](https://stackoverflow.com/questions/26596123/internet-explorer-9-10-11-event-constructor-doesnt-work) –  Oct 09 '18 at 14:02
  • 1
    `event = event || window.event;` That makes no sense why it is there.... – epascarello Oct 09 '18 at 14:04
  • For `event = event || window.event`, you need `event` as a formal parameter to the function. – Pointy Oct 09 '18 at 14:04
  • event = event || window.event makes no difference to the code. The result is still that Event is not defined. How is it possible to define it within the code? – MPorter Oct 09 '18 at 14:19
  • Did you look at the dupe? – epascarello Oct 09 '18 at 14:27
  • yeah the dupe includes createEvent which is not supported – MPorter Oct 10 '18 at 12:23

1 Answers1

0

See the comments in the code.

function categoryClick() {
  var events = ["mousemove", "mouseover", "focus", "mousedown", "mouseup", "click"];
  var combobox = document.getElementById("category");
  var a = combobox.childNodes;
  var target = a[0].childNodes[0];

    for (var i = 0; i < events.length; i++) {
    if (typeof(Event) === 'function') { // just as you did
        var eventObject = new Event(events[i], {
          "bubbles": true,
          "cancelable": false
        })
    } else { // fallback for IE
        var eventObject = document.createEvent('Event');
        eventObject.initEvent(events[i], true, false);
    }

    target.dispatchEvent(eventObject);
  }
}
connexo
  • 53,704
  • 14
  • 91
  • 128