Reputation: 71
I have the code below in my website and integrated a range slider using HTML5. I want the slider to update the text-input with the value and vice versa. Would I do this using jQuery? If so can you give me example code?
Thanks.
<input type="text" name="chance" id="chance" class="text" value="50">
<input type="range" id="chance" class="vHorizon" min="0.01" max="98" style="background-color: #00aec8; width: 50%;">
Upvotes: 5
Views: 29543
Reputation: 478
An improvement I found useful
$('#chanceSlider').on('input change', function(){
$('#chance').val($('#chanceSlider').val());
});
jsfiddle http://jsfiddle.net/tDkEW/408/
Upvotes: 5
Reputation: 1578
If you want to refresh the value in real time use mousemove
, or you can just use change
but it's not at real time.
$('yourselector').on('mousemove change',function() {
//your code
});
Upvotes: 5
Reputation: 1427
Yes, you can do it with html5 elements and jQuery, like this:
HTML:
<input type="text" name="chance" id="chance" class="text" value="50">
<input type="range" id="chance_slider" class="vHorizon" min="0.01" max="98" style="background-color: #00aec8; width: 50%;">
JS:
var slider = $('#chance_slider'),
textbox = $('#chance');
slider.change(function() {
var newValue = parseInt($(this).val());
textbox.val(newValue);
});
textbox.change(function() {
var newValue = parseInt($(this).val());
slider.val(newValue);
});
EDIT
To allow decimals:
var slider = $('#chance_slider'),
textbox = $('#chance');
slider.change(function() {
var newValue = $(this).val();
textbox.val(newValue);
});
textbox.change(function() {
var newValue = $(this).val();
slider.val(newValue);
});
Upvotes: 1
Reputation: 914
You can use change
event on slider and keyup
event on textbox to update the values.
HTML
<input type="text" name="chance" id="chance" class="text" value="50">
<input type="range" id="chanceSlider" class="vHorizon" min="0.01" max="98" step="0.01" style="background-color: #00aec8; width: 50%;">
jQuery
$('#chanceSlider').on('change', function(){
$('#chance').val($('#chanceSlider').val());
});
$('#chance').on('keyup', function(){
$('#chanceSlider').val($('#chance').val());
});
And jsfiddle to play with
Upvotes: 12
Reputation: 1162
Make your ids unique first.
HTML
<input type="text" name="chance" id="display" class="text" value="50">
<input type="range" id="slider" class="vHorizon" min="0.01" max="98" style="background-color: #00aec8; width: 50%;">
JQuery
$('#slider').on('change',function(){
$('#display').val($('#slider').val());
});
Upvotes: 0
Reputation: 3183
I suggest caching the jquery selectors to improve performance.
Also ids must be unique, see fiddle.
var $chance_txt = $('#chance_txt'),
$chance_slider = $('#chance_slider').on('change', function() {
$chance_txt.val($chance_slider.val());
});
Upvotes: 0
Reputation: 1654
You could definitely use jQuery.
$('input#chance').on('change', function() {
$('input[name=chance]').val($(this).val());
});
$('input[name=chance]').on('change keyup', function() {
$('input#chance').val($(this).val());
});
Upvotes: 2