I use a range slider with a text box, so whenever I move a range, the value becomes updated in the text box. Also, when I change the value in the text box, the slider will move accordingly.
Using javascript, I also change the bottom color of the slider to orange. This works fine when I move the range manually, but I encounter a problem when moving the slider, it depends on updating the value of the text field manually. The slider moves, but the color of the bottom is not updated. I think this is because the change function with a range does not start. Could you tell me how I can start it with updating a text field?
My code is:
</head> </head> <body> <input type="range" min="1" max="100" step=".1" value="15"id="myrange" class="myrange"> <input type="text" id="priceText"></input> </body>
JavaScript:
$(function() { var style = $("<style>", {type:"text/css"}).appendTo("head"); $('#priceText').val($(myrange).val()); $('input[type="range"]').on('input change', function() { var val = ($(this).val() - $(this).attr('min')) / ($(this).attr('max') - $(this).attr('min')); $('<style type="text/css"></style>').text('input[type="range"]::-webkit-slider-runnable-track { background-image:-webkit-gradient(linear, left top, right top, color-stop('+val+', orange), color-stop('+val+', #C5C5C5));}').appendTo('head'); $('#priceText').val($(this).val()); }); }); $('#priceText').keyup(function(e){ var val = $(this).val().replace(/\D/g,'');
Fiddle: https://fiddle.jshell.net/anoopcr/o07tu661/3/
source share