tags:

views:

1168

answers:

1

How can I trigger a change event on a jQuery UI slider?

I thought it would be

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

but that does nothing.

Full example script follows:

<link href="http://jqueryui.com/latest/themes/base/ui.all.css" rel="stylesheet" type="text/css"> 

<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.js" type="text/javascript"></script> 
<script src="http://jqueryui.com/latest/ui/ui.core.js" type="text/javascript"></script> 
<script src="http://jqueryui.com/latest/ui/ui.slider.js" type="text/javascript"></script> 

<body>

<div id="slider"></div>

<script type="text/javascript">

$().ready(function()
{
    $('#slider').slider({change: function() { alert(0); }});

    // These don't work
    $('#slider').trigger('change');
    $('#slider').trigger('slidechange');
});
</script>

I would expect this to alert "0" when the page loads

+6  A: 
redsquare
Bet you this does the trick, nice screenshot :)
karim79
+1 - voted up also
karim79
Eww ugly :p +1 though 'cause it works. I'll give it a bit longer in case John Resig stops by...
Greg
hmm, not sure he will know. Maybe better as a jquery-ui google group post / trac issue. Not sure if they considered this used case?
redsquare
I've edited your answer because while it worked for my test script it didn't work for the real one - "this" was coming out wrong.
Greg
This is not a solution. Calling the function associated with the event is not the same as triggering the event itself, e.g. you have to manually pass the parameters (even if this case it's only the object, other events may have more parameters)
Patonza
@Patonza Care to provide a better solution....sheesh
redsquare