Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to find what caused onChange event for select box in IE - keyboard or mouse?
    text
    copied!<p>Is there some way to find out what caused the onChange event on select box in Internet Explorer (>= IE8) - keyboard or mouse?</p> <p>I have a code which doing something when user selecting a value, and this code works great in Firefox and Chrome but not in IE (no surprise, huh). In IE it works fine only if user uses mouse but not a keyboard, because then it fires a onchange event on every keypress (not on Enter as normal browsers).</p> <p>So, to fix this behavior I need to know if event is fired using a keyboard and then I will filter it.</p> <p><strong>Update:</strong></p> <p>Ok, after playing a bit I found a good solution. Posting it here in case someone will find it useful. Solution below using jQuery but it can be done in pure Javascript too.</p> <p>This is a code which caused a problem:</p> <pre><code>$("#mySelectBox").change(function () { // Do something }); </code></pre> <p>And this is my solution. It's probably not perfect, but it works in my case. And event handlers could be chained in jQuery, of course. The code below stores initial value of the select and uses it to avoid doing something on initial mouse click - when user expands a select box. Also it filters all keypresses except Enter.</p> <pre><code>function doSomething(el) { if (el.data["valueOnFocused"] !== el.val()) { // Do something } } $("#mySelectBox").focusin(function () { $(this).data["valueOnFocused"] = $(this).val(); }); $("#mySelectBox").keyup(function (e) { if (e.which === 13) { doSomething($(this)); } }); $("#mySelectBox").click(function () { doSomething($(this)); }); </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload