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;
}
Andrew Willem's solutions are not mobile device compatible.
Here's a modification of his second solution that works in Edge, IE, Opera, FF, Chrome, iOS Safari and mobile equivalents (that I could test):
Update 1: Removed "requestAnimationFrame" portion, as I agree it's not necessary:
Update 2: Response to Andrew's 2nd Jun 2016 updated answer:
Thanks, Andrew - that appears to work in every browser I could find (Win desktop: IE, Chrome, Opera, FF; Android Chrome, Opera and FF, iOS Safari).
Update 3: if ("oninput in slider) solution
The following appears to work across all the above browsers. (I cannot find the original source now.) I was using this, but it subsequently failed on IE and so I went looking for a different one, hence I ended up here.
But before I checked your solution, I noticed this was working again in IE - perhaps there was some other conflict.
You could use the JavaScript "ondrag" event to fire continuously. It is better than "input" due to the following reasons:
Browser support.
Could differentiate between "ondrag" and "change" event. "input" fires for both drag and change.
In jQuery:
Reference: http://www.w3schools.com/TAgs/ev_ondrag.asp
Apparently Chrome and Safari are wrong:
onchange
should only be triggered when the user releases the mouse. To get continuous updates, you should use theoninput
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:Check out this Bugzilla thread for more information.
UPDATE: I am leaving this answer here as an example of how to use mouse events to use range/slider interactions in desktop (but not mobile) browsers. However, I have now also written a completely different and, I believe, better answer elsewhere on this page that uses a different approach to providing a cross-browser desktop-and-mobile solution to this problem.
Original answer:
Summary: A cross-browser, plain JavaScript (i.e. no-jQuery) solution to allow reading range input values without using
on('input'...
and/oron('change'...
which work inconsistently between browsers.As of today (late Feb, 2016), there is still browser inconsistency so I'm providing a new work-around here.
The problem: When using a range input, i.e. a slider,
on('input'...
provides continuously updated range values in Mac and Windows Firefox, Chrome and Opera as well as Mac Safari, whileon('change'...
only reports the range value upon mouse-up. In contrast, in Internet Explorer (v11),on('input'...
does not work at all, andon('change'...
is continuously updated.I report here 2 strategies to get identical continuous range value reporting in all browsers using vanilla JavaScript (i.e. no jQuery) by using the mousedown, mousemove and (possibly) mouseup events.
Strategy 1: Shorter but less efficient
If you prefer shorter code over more efficient code, you can use this 1st solution which uses mousesdown and mousemove but not mouseup. This reads the slider as needed, but continues firing unnecessarily during any mouse-over events, even when the user has not clicked and is thus not dragging the slider. It essentially reads the range value both after 'mousedown' and during 'mousemove' events, slightly delaying each using
requestAnimationFrame
.Strategy 2: Longer but more efficient
If you need more efficient code and can tolerate longer code length, then you can use the following solution which uses mousedown, mousemove and mouseup. This also reads the slider as needed, but appropriately stops reading it as soon as the mouse button is released. The essential difference is that is only starts listening for 'mousemove' after 'mousedown', and it stops listening for 'mousemove' after 'mouseup'.
Demo: Fuller explanation of the need for, and implementation of, the above work-arounds
The following code more fully demonstrates numerous aspects of this strategy. Explanations are embedded in the demonstration:
I'm posting this as an answer because it deserves to be it's own answer rather than a comment under a less useful answer. I find this method much better than the accepted answer since it can keep all the js in a separate file from the HTML.
Answer provided by Jamrelian in his comment under the accepted answer.
Just be aware of this comment by Jaime though
As in it will fire the event when you have stopped moving the mouse, and then again when you release the mouse button.
For a good cross-browser behavior, less and understandable code, best is to use the onchange attribute in combination of a form:
This is a html/javascript only solution, and can as well be used in-line.