How can I trigger a change event on a jQuery UI slider?
I thought it would be
$(\'#slider\').trigger(\'slidechange\');
but that do
I've hit this problem recently, and used Felipe Castro's comment-solution, with a necessary change to set the context right:
$slider.slider('option', 'slide').apply($slider, [null, {value: $slider.slider('value')}])
Yet another way, which is handy for UI widgets like autocomplete, is to access the event directly via the data method. JQuery stores all of its event hookup info using .data("events"), so if you use that property you can access the events directly. On some UI components (eg. autocomplete) the events aren't even attached to the main element, they're attached to the UI object itself. Luckily, that UI object is also available in data.
So, without further ado, here's how you'd invoke the autocomplete's select event:
$("#autoComplete").data("autocomplete").menu.select()
Or, (getting back to sliders) to trigger a slider's change:
$("#slider").data("slider")._change()
Trigger is still the best way of course, since it actual utilizes the event system, but on the more complex widgets where "$(elem).trigger" won't be enough, this approach is handy.
* EDIT *
Just figured out that you actually can "trigger" the event properly with this method, using the widget's "secret" _trigger property. For example:
$("#autoComplete").data("autocomplete")._trigger("select", fakeEvent, fakeItem)
Hope this helps someone.
This maybe resurrecting an old thread, but was just having a similar experience. The solution that I came up with (because the thought of calling slider(...)
multiple times was not appealing):
$slider.slider('option', 'slide').call($slider, event, ui);
With $slider
being bound to the $(selector).slider(...)
initialization.
As documentation;
change( event, ui ) Triggered after the user slides a handle, if the value has changed; or if the value is changed programmatically via the value method.
Just setup bind change event
$(".selector").slider({change: function(event, ui) {console.log('It Works!'}});
and set value
$(".selector").slider('value',0);
I know this is way past the post date, but I wanted to post to provide this solution to anyone that stumbles upon this post.
You can accomplish event triggering by doing something along the lines of:
//TO DEFINE SLIDER AND EVENTS
$('#slider').slider().bind('slidechange',function(event,ui){...});
//TO TRIGGER EVENT
$('#slider').trigger('slidechange');
Unfortunately you cannot define the functions as options within the init object, however, I think it ends up looking cleaner and is more straight forward and correct than the other answer using the call method (i.e. it does use the event system).
And yes, the callbacks you define here will be called on normal operation (changing the position of the slider in this case) as it normally would. Just make sure you use the correct event type names from the UI documentation.
If you want to add multiple events at once, remember you can provide an object to bind using the event names as keys:
//TO DEFINE SLIDER AND EVENTS
$('#slider').slider().bind({
slidestart : function(event,ui) {...},
slidechange : function(event,ui) {...},
slidestop : function(event,ui) {...},
});
//TO TRIGGER EVENTS
$('#slider').trigger('slidestart');
$('#slider').trigger('slidechange');
$('#slider').trigger('slidestop');
This is noted in the documentation, although, it is not very clear. Took me developing a couple plugins on my own to really understand the UI event system.
Enjoy
A good approach is simply to change the value of the slider. The slider's own change method should then respond to the change of value. For instance:
var newValue=5;
$('#slider').slider("value", newValue);