How to restrict HTML range slider from moving beyond a range - html

I'm using a range slider with range 0 to 100 and the current value is set to max.
My objective is that I have a limiting value of 50 so that the slider could be moved between 50 and 100 and not 0 to 50. I have setup a jQuery handler as below, however I'm still able to move backwards from 50.
Is there a way to prevent the slider from going backwards from 50?
$(document).on("input change", "#range", function() {
var scrolled_value = $(this).val();
var limiting_value = 50;
if (scrolled_value <= limitng_value)
return false;
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<input id="range" type="range" name="range" min="0" value="100" max="100" />

You can set the value in the change event to stop it going below that value:
if (scrolled_value <= limiting_value)
$(this).val(limiting_value);
Giving an updated snippet (with as few changes as possible to the original):
$(document).on("input change", "#range", function() {
var scrolled_value = $(this).val();
var limiting_value = 50;
if (scrolled_value <= limiting_value)
$(this).val(limiting_value);
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<input id="range" type="range" name="range" min="0" value="100" max="100" />

You can solve this issue by setting the sliders value to 50 when it's below 50, like shown below:
const input = document.querySelector("input");
const sliderLimit = 50;
input.addEventListener("input", () => {
if (input.value < sliderLimit)
input.value = sliderLimit;
})
<input type="range" min="0" value="100" max="100" />

Minimal solution using jQuery (like OP) while also limiting the value on first-render
$('input').on("input change", function() {
if (this.value > 50 )
this.value = 50
}).trigger('change')
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<input type="range" min="0" value="100" max="100" />
By the way, I've created a beautiful CSS-only range slider:
https://github.com/yairEO/ui-range

Related

Do something when one input range slider value is less than the other (using jQuery)

I am trying to set the background color of two form input range sliders using jQuery. Users are selecting a minimum and maximum distance, so I would like to alert the user when the minimum distance is larger than the maximum distance. I've tried this using jQuery but the script below does not do the trick for me.
These are my two input range sliders:
<input id="mindistance" class="rangeslider" type="range" name="mindistance" min="0" max="100" value="0" step="5">
<input id="maxdistance" class="rangeslider" type="range" name="maxdistance" min="0" max="100" value="100" step="5">
This is my jQuery at the moment:
$('#maxdistance').blur(function(){
if ( $(this).val().length <= $('$mindistance').val().length ) {
$(this).css({ 'background-color': 'rgb(255,175,175)' });
$('#mindistance').css({ 'background-color': 'rgb(255,175,175)' });
} else {
$(this).css({ 'background-color': '' });
$('#mindistance').css({ 'background-color': '' });
}
});
I've tried .focusout() instead of .blur() as well, so I suspect the <= operator not working here but I'm not sure. Does anybody know how to do this?
EDIT: both .val() and .val().length don't do the trick.
EDIT: also tried $(document).on('input', '#mindistance', function() { /* check values */ });
First of all, you have an error trying to reference maxdistnce.
'$maxdistance' should be '#maxdistance'
i changed a few other things
Placed your sliders in divs and changed the bgcolor of these divs instead of the sliders themselves
reset the bg colors each time the event occurs
used the on change event instead of blur (not necessary)
removed .val().length and used .val() alone
<div class="mindistance">
<label>min</label>
<input id="mindistance" class="rangeslider" type="range" name="mindistance" min="0" max="100" value="0" step="5">
</div>
<div class="maxdistance">
<label>max</label>
<input id="maxdistance" class="rangeslider" type="range" name="maxdistance" min="0" max="100" value="100" step="5">
</div>
$(document).on('change', '#mindistance',function(e){
console.log($(this).val(), $('#maxdistance').val());
$('.mindistance').css({ 'background-color': '' });
$('.maxdistance').css({ 'background-color': '' });
if ( $(this).val() <= $('#maxdistance').val() ) {
$('.mindistance').css({ 'background-color': 'rgb(255,175,175)' });
$('.maxdistance').css({ 'background-color': 'rgb(255,175,175)' });
}
});

Variable outputs - Html5 range slider

I want to have the range slider make different calculations. Say for instance when I drag the slider to 50 its calculating at (0.10*this.value) , dragged to 100 it needs to be (0.08*this.value) , 200 to (0.07*this.value) , etc. Would this be possible?
<input type="range" name="grade" orient="vertical min="0" max="1000" value="0" step="50" onchange="showValue(0.10*this.value) ">
<p><span id="range">0</span>
x 10 kg Bags of ice suggested</p>
<script type="text/javascript">
function showValue(newValue)
{
Math.round(7.000000000000001)
document.getElementById("range").innerHTML=Math.round(newValue);
}
</script>
<input type="range" name="grade" orient="vertical min="0" max="1000" value="0" step="50" onchange="showValue(this.value) ">
And then you can use a switch in your function showValue() :
function showValue(_value) {
switch(value) {
case 100:
//DO STG
break;
}
}

Flipped Range/Slider input html

Does anyone know how to achieve this?
so eg. it starts from max to low (left to right):
---------------------||---------------------
100__________50__________20
Check this out JSFiddle Link.
HTML Part:
<input id="slider2" type="range" min="0" max="100" step="50" value="0" >
<input id="rangeValue2" type="text" size="2">
JavaScript Part:
var max_range = 100;
var range_element = document.getElementById("rangeValue2");
var range_input_element = document.getElementById("slider2");
range_element.value = max_range - range_input_element.value;
document.getElementById("slider2").onchange=function(){
range_element.value = max_range - range_input_element.value;
};
Note: Max range should be same in HTML and JavaScript.
Like in html it is set as max="100" and in JavaScript it is set as var max_range = 100;

HTML5 Number Input Fails with decimal value

I have not confirmed if this is just an iOS error, but I'm facing this problem in my app.
This:
<input type="number" step="0.01" disabled="disabled" name="cash_total" value="4.00" id="cash_total" placeholder="0.00" />
Shows: 4
Even with the Step in there. What's going on?!
UPDATE: This is a Safari bug I've noticed, even on desktop: http://jsfiddle.net/S9msN/
This might work for you. When the page loads, it uses some js to do the math and then adjust the value: http://jsfiddle.net/LSnvj/
<script type="text/javascript">
<!--
jQuery(document).ready(
function(){
jQuery(window).load(function(){
var value = 4;
var step = 0.01;
var total = value+step;
jQuery("#cash_total").attr("value",total);
})
});
-->
</script>
<input type="number" step="0.01" disabled="disabled" name="cash_total" value="0.00" id="cash_total" placeholder="0.00" />

How do I show the value beside an HTML5 range input type?

Okay so I am trying to use range for input type
Times: <input type="range" name ="times"><br>
But I want the value to be printed next to it, how can I do this?
Like
-------*-- 7
*--------- 1
attach to it onChange event
and in the event handler function you can select the value of the
and write it as innerHtml to a span beside the controller
<input id="mine" type="range" min="-100" max="100" value="0" step="10" onChange="change();"> <span id="result"></span>
<script>
var result = document.getElementById("result");
var mine = document.getElementById("mine");
function change(){
result.innerText = mine.value;
}
</script>
function showCount(){
var count = document.getElementById("slider").value;
console.log("meter position : "+count);
}
<input type="range" value="0" id="slider" step="1" min="0" max="15" onchange="showCount()"/>
This will give the count when you will move the sliderenter code here or put and alert.