Why doesn't $("#slider").trigger("slide") result in calling the slide handler?

kjo picture kjo · Oct 5, 2013 · Viewed 11.4k times · Source

I have a jQuery slider (actually, a custom extension thereof) equipped with a handler for the slide event, and this handler gets properly called when the slider is manipulated interactively, but not when I run

$("#slider").trigger("slide");

This command runs without error, but the slide handler does not get called.

Why is this?

Do I need to pass a different argument to trigger to cause the slide handler to be called?

EDIT: changed post's title to more closely reflect the question's motivation.

Answer

Tats_innit picture Tats_innit · Oct 5, 2013

Working demo: http://jsfiddle.net/fLnmN/ or http://jsfiddle.net/F5ZCK/

Use .bind like I used in the demo. you can use .on as well like this: http://jsfiddle.net/u82Y5/

Some really helpful post here: Trigger a jQuery UI slider event

This should fit your need, :)

code

$('.slider').slider().bind({
    change: function() {
       alert('change triggered');
    }
});

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

OR

$('.slider').slider().on({
    change: function() {
       alert('change triggered');
    },
    slidechange: function() {
        alert('slide trigger');
    }
});

$('.slider').trigger('change');
$('.slider').trigger('slidechange');