Search code examples
javascripthtmlfirefoxinputonchange

onchange event on input type=range is not triggering in Firefox while dragging


When I played with <input type="range">, Firefox triggers an onchange event only if we drop the slider to a new position where Chrome and others triggers onchange events while the slider is dragged.

How can I make it happen on dragging in Firefox?

function showVal(newVal){
    document.getElementById("valBox").innerHTML=newVal;
}
<span id="valBox"></span>
<input type="range" min="5" max="10" step="1" onchange="showVal(this.value)">


Solution

  • Apparently Chrome and Safari are wrong: onchange should only be triggered when the user releases the mouse. To get continuous updates, you should use the oninput event, which will capture live updates in Firefox, Safari and Chrome, both from the mouse and the keyboard.

    However, oninput is not supported in IE10, so your best bet is to combine the two event handlers, like this:

    <span id="valBox"></span>
    <input
      type="range"
      min="5"
      max="10"
      step="1"
      oninput="showVal(this.value)"
      onchange="showVal(this.value)"
    />
    

    Check out this Bugzilla thread for more information.