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?
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});
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
.
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");
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With