Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to prevent the user to drag HTML input type='range' right?

I have three input type='range' fields that have different max values but share the same capacity. This capacity is decreased with every change on either of the three inputs. When the capacity reaches 0 I am supposed to prevent the input fields from moving right ( from increasing their value ) and only move left, but I dont know how to do it using JS and jQuery

here is the html for the inputs

<input type="range" class="slider" id="woodSlider" min="0" value="0" max="1558">
<input type="range" class="slider" id="ironSlider" min="0" value="0" max="2555">
<input type="range" class="slider" id="stoneSlider" min="0" value="0" max="2451">

and here is the code that decreases the capacity:

$("input").change(function() {  
    $("#capacityLeft").html(parseInt(holding.capacity) - 
         $("#woodSlider").val() -
         $("#ironSlider").val() -
         $("#stoneSlider").val());
    if(parseInt($("#capacityLeft").html()) <= 0) {
    // TODO: FIND OUT HOW TO STOP THE SLIDERS FROM MOVING
    }
});
like image 408
vank091 Avatar asked Sep 30 '13 08:09

vank091


People also ask

How do I limit input range in html?

Complete HTML/CSS Course 2022 The HTML <input> tag is used to get user input in HTML. To give a limit to the input field, use the min and max attributes, which is to specify a maximum and minimum value for an input field respectively. To limit the number of characters, use the maxlength attribute.

How do I disable input type range?

The DOM Input Range disabled Property is used to set or return whether the Input range Field must be disabled or not. A disabled range Field is un-clickable and unusable. It is a boolean attribute and used to reflect the HTML Disabled attribute. It is usually rendered in grey color by default in all the Browsers.

How do I customize my input range?

To style the range input with CSS you'll need to apply styles to two pseudo-elements: ::-webkit-slider-thumb and ::-webkit-slider-runnable-track . Find out how you can apply custom styling and make the range input more functional and appealing. Contents of the article: CSS selectors for the range input.


1 Answers

Okay, so you can use event.preventDefault() to stop an event. You should bind a function to onchange or oninput events on these range sliders and calculate the sum, check if it exceed the max, then stop the event if it does.

Here's a pure JS solution (fiddle link), could be easily rewritten with jQuery.

var maxTotal = 150, // define the max sum of values
    inputs = [].slice.call(document.getElementsByTagName('input')), // refrence to the elements
    getTotal = function(){ // helper function to calculate the sum
        var sum = 0;
        inputs.forEach( function(input){
           sum += parseInt(input.value, 10); 
        });
        return sum;
    },
    maxReached = function(e){  // check if the max is reached
        var sum = getTotal(), target;
        if(sum > maxTotal){
            target = e.target;
            // set the max possible value if the user, for example, clicks too far to the right
            target.value = target.value - (sum - maxTotal);
            // next line is just for demonstrational purposes
            document.getElementById('total').innerHTML = getTotal();

            // prevent increasing the value
            e.preventDefault();
            return false;
        }
        // next line is just for demonstrational purposes
        document.getElementById('total').innerHTML = getTotal();

        // everything's fine, nothing to do.
        return true;
    };

// attach the maxReached function to your inputs
inputs.forEach( function(input){
    input.addEventListener('input', maxReached );
});
like image 138
pawel Avatar answered Oct 06 '22 00:10

pawel