Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

HTML Range Slider and jQuery?

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%;">
like image 545
Chuck Bartowski Avatar asked Jul 05 '13 06:07

Chuck Bartowski


2 Answers

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

http://jsfiddle.net/tDkEW/1/

like image 174
Ilu Avatar answered Nov 06 '22 22:11

Ilu


An improvement I found useful

$('#chanceSlider').on('input change', function(){ $('#chance').val($('#chanceSlider').val()); });

jsfiddle http://jsfiddle.net/tDkEW/408/

like image 34
6by3 Avatar answered Nov 06 '22 23:11

6by3