stax stax - 3 months ago 6
jQuery Question

Jquery range slider if else in .slider

In my projet i have some form where the user use range slider. User can modify the data of the form after his first submit. So when he is updating, the range slider start with the value chosen the last time.
I did this like that, but i want to know if there is a more elegant way to do this, I tried to use if/else in .slider but it seems like I can't do it.

Working code :

$(function() {

if (key != null) {
$("#sliderGar")
.on("slidecreate", function test(e, ui){
/****/
})

.slider({
min: 0,
max: jsonValRecons.length-1,
value: key

})


.slider("pips", {
rest: "label",
labels: jsonValRecons
})

.on("slidechange", function(e, ui){
/****/

})
}


});

$(function() {

if (key == null) {
$("#sliderGar")
.on("slidecreate", function test(e, ui){
/****/
})

.slider({
min: 0,
max: jsonValRecons.length-1

})


.slider("pips", {
rest: "label",
labels: jsonValRecons
})

.on("slidechange", function(e, ui){
/****/

})
}


});

Answer

You can use if/else outside the slider and just try something like this:

if(key!=null)
    slideValue = key
else
    slideValue = 0

And then go on with your slider initialization :

    /**/
    .slider({
        min: 0,
        max: jsonValRecons.length-1,
        value: slideValue
    })
    /**/