Alain Bruno
Alain Bruno

Reputation: 137

Is it possible to change the min and max of a HTML5 range slider according to the input of another form?

Refer to this: http://www.alainbruno.nl/form.html

So, for instance, when the race selected is Human I want the minimum and maximum of the age slider to be between 10-80, but when Faela is chosen I want it to be between 10-70. Is this possible, and if so, how?

If possible I would like it to work without submitting the race input before the slider changes.

Upvotes: 3

Views: 7514

Answers (3)

LuiNova
LuiNova

Reputation: 396

I see that others have already answered your question using great answers. This is my first try at Stackoverflow and hence my first answer. I am not so advanced with JQuery or AJAX but I used a simple Javascript.

<!DOCTYPE html>
<html lang="en">
<head>
<script>
function changeRange(x,y) {
var input = document.getElementById("slider");
input.setAttribute("min", x);
input.setAttribute("max", y);
}
</script>
</head>
<body>
<h1>Form</h1>
<form>
<fieldset>
<legend>Appearance</legend>
<p>
<label>
Select Race:
</label>
<select id="Race" onchange="changeRange('10', '80')">
<option value="human">Human</option>
<option value="faela" >Faela</option>
<option value="domovoi">Domovoi</option>
<option value="arcon">Arcon</option>
<option value="tsaaran">Tsaaran</option>
</select>
</p>
<p>
<label>Select Gender</label>
<select id="Gender">
<option value="male">Male</option>
<option value="female">Female</option>
<option value="other">Other</option>
</select>
</p>
<p>
<label for="range">Select Age:</label> <input type="range" min="14" max="60" id="slider" value="10" name="range"> <span id="output"></span>
</p>
</fieldset>
</form>
</body>
</html>

Not the best answer but I wanted to give it a try :)

Upvotes: 2

faino
faino

Reputation: 3224

Of course, through the use of objects, you can make this quick and painless, something like:

<!DOCTYPE html>
<html lang="en" dir="ltr">
    <head>
        <title>Character Creation</title>
        <meta charset="UTF-8" />
        <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js"></script>
        <script>
            $(function(){
                //Range
                var slider = $("#slider"), val = slider.val(), output = $("#output");
                output.html(val);
                slider.on("change", function(){
                    output.html($(this).val());
                });
                //Change range values
                var minMax = {
                    human: [10, 80],
                    faela: [10, 70],
                    domovoi: [12, 99],
                    arcon: [5, 80],
                    tsaaran: [20, 100]
                };
                $("#Race").on("change", function(){
                    var ages = minMax[$(this).val()];
                    if(ages) {
                        slider.attr({
                            min: ages[0],
                            max: ages[1]
                        });
                        output.html(slider.val());
                    }
                });
            });
        </script>
    </head>
    <body>
        <h1>Form</h1>
        <form>
            <fieldset>
                <legend>Appearance</legend>
                <p>
                    <label for="Race">Select Race:</label>
                    <select name="Race" id="Race">
                        <option value="human">Human</option>
                        <option value="faela">Faela</option>
                        <option value="domovoi">Domovoi</option>
                        <option value="arcon">Arcon</option>
                        <option value="tsaaran">Tsaaran</option>
                    </select>
                </p>
                <p>
                    <label for="Gender">Select Gender</label>
                    <select name="Gender" id="Gender">
                        <option value="male">Male</option>
                        <option value="female">Female</option>
                        <option value="other">Other</option>
                    </select>
                </p>
                <p>
                    <label for="slider">Select Age:</label> <input name="slider" type="range" min="14" max="60" id="slider" value="10" />
                    <span id="output"></span>
                </p>
            </fieldset>
        </form>
    </body>
</html>

Upvotes: 2

j08691
j08691

Reputation: 207861

Sure, try adding this:

$('#Race').change(function () {
    if (this.value == "faela") {
        $('#slider').prop({
            'min': 10,
            'max': 70
        });
    }
    if (this.value == "human") {
        $('#slider').prop({
            'min': 10,
            'max': 80
        });
    }
    $('#slider').val(10);
    output.html('10');
});

jsFiddle example

(note that in your code your slider started with a min/max of 14 and 60 so I changed that to 10 and 80.)

Upvotes: 5

Related Questions