JQuery UI slider - value slider 2 determined value slider 1

360 views Asked by At

Ive been trying to get with JQuery UI slider the following to work.

Ive got 2 sliders, one is a amount the other is a duration. When the amount is lower then 200 you can select 15 and 30 days. If the amount is higher than 200 only 30 days and the slider should disable.

Ive been trying a lot, you can see my experiments at jsfiddle. http://jsfiddle.net/prommetheus/jatcrsxv/

$('#howmuch_sld').slider({
    range: "min",
    value: 300,
    min: 50,
    step: 50,
    max: 600,
    slide: function(event, ui) {
        $('#amt').html('€' + ui.value);
    }
});
$('#amt').html('€' + $('#howmuch_sld').slider('value'));

$('#howlong_sld').slider({
    range: "min",
    value: 15,
    min: 15,
    step: 15,
    max: 30,
    slide: function(event, ui) {
        if($('#howmuch_sld').slider('value') > '200'){
            $('#howlong_sld').slider({
                value: 30,
                min: 15,
                step: 15,
                max: 45, // TO CENTER SLIDER HANDLER
                disabled: true,
            });
        };
        $('#day').html(ui.value + '<span class="small"> days</span>');
    }
});

$('#day').html($('#howlong_sld').slider('value') + '<span class="small"> days</span>');

I hope someone can help me on the right way!

1

There are 1 answers

0
Julien Grégoire On BEST ANSWER

From what I understand, your validation is in the wrong slide event. It should be during the amount slide since that's what will have an effect. And instead of redoing the slider on slide, you can change the options after it's been created. Something like this should get you closer to your objective:

$('#howlong_sld').slider({
    range: "min",
    value: 15,
    min: 15,
    step: 15,
    max: 30,
    slide: function(event, ui) {
        $('#day').html($('#howlong_sld').slider('value') + '<span class="small"> days</span>');
    },
    //slide happens only when handle is actually moving, so it's always good
    //to update on stop as well 
    stop: function(event, ui) {
        $('#day').html($('#howlong_sld').slider('value') + '<span class="small"> days</span>');
    },
});

$('#howmuch_sld').slider({
    range: "min",
    value: 300,
    min: 50,
    step: 50,
    max: 600,
    slide: function(event, ui) {
        $('#amt').html('€' + ui.value);
        //You check your condition while amount is sliding.
        if($('#howmuch_sld').slider('value') > 200){
            //this is how you set variable after creation
            $('#howlong_sld').slider('option', 'value', 30);
            //you can also call methods
            $('#howlong_sld').slider('disable');

        } else {

            $('#howlong_sld').slider('enable');
        }
        $('#day').html($('#howlong_sld').slider('value') + '<span class="small"> days</span>');
    }
});
$('#amt').html('€' + $('#howmuch_sld').slider('value'));



$('#day').html($('#howlong_sld').slider('value') + '<span class="small"> days</span>'); 

http://jsfiddle.net/tmcyzcvc/1/