user765368
user765368

Reputation: 20356

cannot move slider if value is not integer jquery ui slider

I have a problem here with the jQuery UI Slider plugin. What I'm trying to accomplish is that if there is not a value for the slider in the database, that means there has been no rating yet, so I want to display something like "No value" or "Nil" or something. The problem is that if that value is not an integer, I cannot move the slider handle. You can view a DEMO HERE.

Why is this happening, any idea what am I doing wrong, why can't I move the slider if the value is not a valid integer.

Upvotes: 0

Views: 1544

Answers (3)

satoshi
satoshi

Reputation: 4113

My suggestion is to associate a number (e.g. -1) with the "No value" state and have some JavaScript logic to help you obtain what you want:

$(".slider").slider({
    range: "min", 
    min: -1, 
    max: 10, 
    slide: function(event, ui){
        var value;
        if(ui.value == -1)
        {
            value = "--";
        }
        else
        {
            value = ui.value;
        }

        $(this).find("input.slider-value").val(value);
    },
    create: function(event, ui){
        $(this).slider("value", $(this).find("input.slider-value").val()); // value of the slider handle
    }
});

<div>
    <p style="color:green;"><b>Slider can be moved</b></p>

    <div class="slider"><br/>
        <input class="slider-value" type="text" value="3" style="text-align: center;" />
    </div>
</div>

DEMO

Upvotes: 0

Selvakumar Arumugam
Selvakumar Arumugam

Reputation: 79850

jQuery slider works based on integer values. What do you want to do when the slider is "not an integer"?

Check out the below code to stay at 0 if it is not a number,

function getSliderVal ($this) {
    var val = $this.val();
    return isNaN(val)?0:val;
}

and in the value param,

    ...
    value: getSliderVal (theSlider1.find("input[id=slider-value-1]")), 

    value: getSliderVal(theSlider2.find("input[id=slider-value-2]")),  
    ...

DEMO

Upvotes: 0

j08691
j08691

Reputation: 208032

It's quite simple. As the docs for the slider say, the value parameter has to be a number and you have "--". Make your value 0 or just nothing (e.g. "") jsFiddle example.

Upvotes: 1

Related Questions