0

guys!

I've been trying to get this solved for awhile, and I'm really stuck, so hoping for your help. This code works in Chrome and in Opera, but doesn't work in IE11. Don't know about other versions of IE. IE JavaScript is enabled. Please take a look, and tell me that I'm doing wrong

<script type="text/javascript"> 
    window.onload = function(e){ 

        var list = document.getElementsByClassName('example');
        switcher_combo = list[0];
        switcher_combo.disabled = true;
        switcher_combo.addEventListener("change", myFunction);

        function myFunction() {
            alert("Changed!")
        } 
    }

    function SetSel(value)
    {
        switcher_combo.value = value;  
        switcher_combo.selected = true;
        switcher_combo.dispatchEvent(new Event('change'));
        return false;
    }
 </script>

      <select id="example1" class='example' name='emaple2'>
        <option value="1">Option one</option>
        <option value="2">Option two</option>
        <option value="3">Option three</option>
      </select>

      <BR>
      <a href="javascript:SetSel(2);">Change Combo to #2</a>
 </body>

Ben
  • 15
  • 4
  • Welcome to Stack Overflow, it would help a lot more when you add the error you are getting (press F12 for developer tools in browser). I think Malaji's answer should work according to the [documentation](https://developer.mozilla.org/en-US/docs/Web/API/Document/createEvent). If it doesn't then please provide code and error by updating your question instead of just stating it doesn't work. – HMR Dec 13 '17 at 13:36
  • Strange though that according to documentation both [dispatchEvent](https://developer.mozilla.org/en-US/docs/Web/API/EventTarget/dispatchEvent#Browser_Compatibility) and [getElementsByClassName](https://developer.mozilla.org/en-US/docs/Web/API/Document/getElementsByClassName#Browser_compatibility) are supported from IE 9 and up. – HMR Dec 13 '17 at 13:46

1 Answers1

1

Duplicate question: dispatchEvent not working in IE11

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);