How can I trigger a change event on a jQuery UI slider?
I thought it would be
$(\'#slider\').trigger(\'slidechange\');
but that do
The jQueryUI Slider documentation gives the following example for triggering an event:
$( ".selector" ).slider({
change: function( event, ui ) {}
});
and for the event trigger:
$( ".selector" ).on( "slidechange", function( event, ui ) {} );
this didn't work for me!
All I had to do to get it working was to change "slidechange"
to "change"
.
$( ".selector" ).on( "change", function( event, ui ) {} );
Hope this helps the future generations that stumbleupon this problem.
Try
$slider = $('#slider');
$slider.slider('option', 'change').call($slider);
Not ideal but gets you working!
I Used both slidechanged and slide, slide triggers when drag the point, slidechanged triggers after release the mouse button (just like click event)
var slider1 = $("#slider1").slider({ min: 1, max: 6 });
//option 1
slider1.on("slide", function (e, ui) {
});
//Option 2
slider1.on("slidechanged", function (e, ui) {
});
Wanted to add a comment on Joey Yore's answer -
I think it's better the other way round
$('#slider').bind({
slidestart : function(event,ui) {...},
slidechange : function(event,ui) {...},
slidestop : function(event,ui) {...},
slidecreate : function(event,ui) {...}
}).slider();
Otherwise some events (namely, 'slidecreate') will be ignored
If you bind to to the slider's change event like that:
$('#slider').change(function() {
alert('action');
});
Then you can trigger it like:
$('#slider').trigger('change');
The solution below mentioned by Joey Yore works too, but the downside is that slidechange
event is not fired (from my experience) when user changes your slider from UI.
//TO DEFINE SLIDER AND EVENTS
$('#slider').slider().bind('slidechange',function(event,ui){...});
//TO TRIGGER EVENT
$('#slider').trigger('slidechange');
I found it easier to use the 'create' method to call the slide or stop function. Eg, for a slider with a min/max range:
$('#height').slider({
...
create: function(event, slider){ $( "#height" ).slider( "option", "values", [1100, 1500] ); handleHeightSlide('slide', $( "#height" ));},
...
});