12

We have a datepicker (in JavaScript) that has a section for checking IE 8 and older and other modern browsers.

if(-1 != navigator.userAgent.indexOf("MSIE")){
obj_caller.target.fireEvent("onchange");
}
else {
var evt = document.createEvent("HTMLEvents");
evt.initEvent("change", false, true);
obj_caller.target.dispatchEvent(evt);
}

It's working fine in Chrome, Firefox, IE8 and below but is failing in IE 11. What I need is a way to get the else part working in IE 11. I just cannot figure out what is failing and how to fix it.

Thanks.

JohnnyCage
  • 175
  • 1
  • 1
  • 11

1 Answers1

11

Your problem is that fireEvent shouldn't be used in newer IE versions. Support for dispatchEvent was added in IE9. http://help.dottoro.com/ljrinokx.php

if(document.createEventObject) {
    obj_caller.target.fireEvent("onchange");
} else {
    var evt = document.createEvent("HTMLEvents");
    evt.initEvent("change", false, true);
    obj_caller.target.dispatchEvent(evt);
}
Ashley Lee
  • 3,810
  • 1
  • 18
  • 26
  • The dispatchevent code is not working in IE 11 and I cannot figure out why. Is anything wrong with the code? I took out the whole if else loop and just tested in IE 11 with the dispatchevent code and it failed. Thanks. – JohnnyCage Dec 26 '14 at 01:48
  • 2
    Still not working. The code inside else is not executing in IE 11. – JohnnyCage Dec 29 '14 at 14:16
  • Works fine for me in all IE (including 11), Chrome, FF. Does this fiddle work for you? http://jsfiddle.net/ashleyglee/vsmac6xL/1/ – Ashley Lee Dec 29 '14 at 14:55
  • The fiddle is working. Let me see if I can find out if something else is causing it to fail. Thanks for your help. – JohnnyCage Dec 29 '14 at 15:30
  • This code works on IE 6 and on IE 11. If you have any problems perhaps you may need a more specific event initialization with initDragEvent initKeyEvent initMessageEvent initMouseEvent initMutationEvent initOverflowEvent initTextEvent initUIEvent – Elmue Jul 16 '15 at 00:26
  • Have you found out what is going on? I'm having exactly the same problem. – Bela Vizy Feb 05 '18 at 04:11