Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jQuery UI Slider (setting programmatically)

I'd like to modify the sliders on-the-fly. I tried to do so by using

$("#slider").slider("option", "values", [50,80]);

This call will set the values, but the element will not update the slider positions. Calling

$("#slider").trigger('change');

does not help either.

Is there another/better way to modify the value AND slider position?

like image 511
jAndy Avatar asked May 14 '10 10:05

jAndy


3 Answers

It depends on if you want to change the sliders in the current range or change the range of the sliders.

If it is the values in the current range you want to change, then the .slider() method is the one you should use, but the syntax is a bit different than you used:

$("#slider").slider('value',50);

or if you have a slider with two handles it would be:

$("#slider").slider('values',0,50); // sets first handle (index 0) to 50
$("#slider").slider('values',1,80); // sets second handle (index 1) to 80

If it is the range you want to change it would be:

$("#slider").slider('option',{min: 0, max: 500});
like image 74
alexteg Avatar answered Nov 03 '22 03:11

alexteg


For me this perfectly triggers slide event on UI Slider :

hs=$('#height_slider').slider();
hs.slider('option', 'value',h);
hs.slider('option','slide')
       .call(hs,null,{ handle: $('.ui-slider-handle', hs), value: h });

Don't forget to set value by hs.slider('option', 'value',h); before the trigger. Else slider handler will not be in sync with value.

One thing to note here is that h is index/position (not value) in case you are using html select.

like image 16
sakhunzai Avatar answered Nov 03 '22 03:11

sakhunzai


None of the above answers worked for me, perhaps they were based on an older version of the framework?

All I needed to do was set the value of the underlying control, then call the refresh method, as below:

$("#slider").val(50);
$("#slider").slider("refresh");
like image 8
Mal Avatar answered Nov 03 '22 03:11

Mal