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?
HTML
<span id="valBox"></span>
<input type="range" min="5" max="10" step="1" onchange="showVal(this.value)">
SCRIPT
function showVal(newVal){
document.getElementById("valBox").innerHTML=newVal;
}
Yet another approach - just set a flag on an element signaling which type of event should be handled:
SUMMARY:
I provide here a no-jQuery cross-browser desktop-and-mobile ability to consistently respond to range/slider interactions, something not possible in current browsers. It essentially forces all browsers to emulate IE11's
on("change"...
event for either theiron("change"...
oron("input"...
events. The new function is......where
r
is your range input element andf
is your listener. The listener will be called after any interaction that changes the range/slider value but not after interactions that do not change that value, including initial mouse or touch interactions at the current slider position or upon moving off either end of the slider.Problem:
As of early June 2016, different browsers differ in terms of how they respond to range/slider usage. Five scenarios are relevant:
The following table shows how at least three different desktop browsers differ in their behaviour with respect to which of the above scenarios they respond to:
Solution:
The
onRangeChange
function provides a consistent and predictable cross-browser response to range/slider interactions. It forces all browsers to behave according to the following table:In IE11, the code essentially allows everything to operate as per the status quo, i.e. it allows the
"change"
event to function in its standard way and the"input"
event is irrelevant as it never fires anyway. In other browsers, the"change"
event is effectively silenced (to prevent extra and sometimes not-readily-apparent events from firing). In addition, the"input"
event fires its listener only when the range/slider's value changes. For some browsers (e.g. Firefox) this occurs because the listener is effectively silenced in scenarios 1, 4 and 5 from the above list.(If you truly require a listener to be activated in either scenario 1, 4 and/or 5 you could try incorporating
"mousedown"
/"touchstart"
,"mousemove"
/"touchmove"
and/or"mouseup"
/"touchend"
events. Such a solution is beyond the scope of this answer.)Functionality in Mobile Browsers:
I have tested this code in desktop browsers but not in any mobile browsers. However, in another answer on this page MBourne has shown that my solution here "...appears to work in every browser I could find (Win desktop: IE, Chrome, Opera, FF; Android Chrome, Opera and FF, iOS Safari)". (Thanks MBourne.)
Usage:
To use this solution, include the
onRangeChange
function from the summary above (simplified/minified) or the demo code snippet below (functionally identical but more self-explanatory) in your own code. Invoke it as follows:where
myRangeInputElmt
is your desired<input type="range">
DOM element andmyListener
is the listener/handler function you want invoked upon"change"
-like events.Your listener may be parameter-less if desired or may use the
event
parameter, i.e. either of the following would work, depending on your needs:or
(Removing the event listener from the
input
element (e.g. usingremoveEventListener
) is not addressed in this answer.)Demo Description:
In the code snippet below, the function
onRangeChange
provides the universal solution. The rest of the code is simply an example to demonstrate its use. Any variable that begins withmy...
is irrelevant to the universal solution and is only present for the sake of the demo.The demo shows the range/slider value as well as the number of times the standard
"change"
,"input"
and custom"onRangeChange"
events have fired (rows A, B and C respectively). When running this snippet in different browsers, note the following as you interact with the range/slider:Demo Code:
Credit:
While the implementation here is largely my own, it was inspired by MBourne's answer. That other answer suggested that the "input" and "change" events could be merged and that the resulting code would work in both desktop and mobile browsers. However, the code in that answer results in hidden "extra" events being fired, which in and of itself is problematic, and the events fired differ between browsers, a further problem. My implementation here solves those problems.
Keywords:
JavaScript input type range slider events change input browser compatability cross-browser desktop mobile no-jQuery