I'm using jQuery 2.1 in Chrome 41. I need to get the values from an input slider as it changes; however, when I use the change event with jquery, I only get the value from the slider after the mouse is released, not during the mousedown and drag.
This small example illustrates the problem:
<input type="range" id="slider" value="0.5" min="0.0" max="1.0" step="0.01" />
<br />
<span id="slider_value">Nothing yet.</span>
<script>
$(document).on('change', '#slider', function() {
$('#slider_value').html( $(this).val() );
});
</script>
I think I could come up with a fix by setting a boolean on mouse up/down events and then getting the values on mousemove events. Is there a cleaner solution?
In modern browsers you can use the input
event:
$(document).on('input', '#slider', function() {
$('#slider_value').html( $(this).val() );
});
Note that IE < 9 does not support it but neither does it support range
input
Reference: MDN input - Event
DEMO
You could listen to the change
/input
events:
Updated Example
The DOM
input
event is fired synchronously when the value of an<input>
or<textarea>
element is changed.
The
change
event is fired for<input>
,<select>
, and<textarea>
elements when a change to the element's value is committed by the user. Unlike theinput
event, the change event is not necessarily fired for each change to an element's value.
$(document).on('input change', '#slider', function() {
$('#slider_value').html( $(this).val() );
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<input type="range" id="slider" value="0.5" min="0.0" max="1.0" step="0.01" />
<br />
<span id="slider_value"></span>
I'm sure listening to the input
event alone would suffice too.
This worked for me. this uses JQuery and JS.
$("#slider").on("input change",(e)=>{
document.getElementById("label").value = `${e.target.value}`;});
It seems to me that selecting DOM elements and modifying attributes/values using the document.getElementById() gives you access to some attributes that JQuery doesnt..
In this case Im able to access the <input>
value and change it accordingly by using the above code but that same code fails when using JQuery's selector... tried to interchangeably append .val
,.text
,.innerText
,.html
on both and only document.getElementById().text
worked.
Im not sure if this has to do with <input>
's attributes but hey it worked.
Also, if you want the input range current value to move with slider, you may try this solution.
As an update to this for 2020 that some may find helpful. I was encountering an instance where I wanted to grab the current value of a slider as it was changed by the user which a lot of these questions answer but as an alternative.
I'm loading jQuery for other functions so this ends up mixing vanilla JS and jQuery but could easily be modified to use only vanilla JS if you're not loading jQuery.
$("#number-slider").mousemove(function(){
$("#slider-value").text(document.getElementById("number-slider").value);
})
The slider has it's own ID where the value is derived from and then that value can be posted into an element with it's own unique ID. You could do whatever you want after grabbing the value though.
Some notes:
getElementById
function if you want to retrieve the value.If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With