views:

21

answers:

1

I can't get before & after callbacks to work with the cycle plugin for jQuery!

I'm not sure what's going wrong, i even tried using the sample code from the documentation.

Here's the code:

$(document).ready(function(){

    function onBefore() { alert('before'); }

    $('.slideshow').cycle({
        before: 'onBefore'
    });
});

and it throws an error: "Error: opts.before[0].apply is not a function"

and in chrome: "Uncaught TypeError: Object onBefore has no method 'apply'"

what is happening!?

A: 

The error is because .apply() is a method on functions, not on strings...and 'onBefore' is a string. Instead, don't use a string...use a direct reference, like this:

$(document).ready(function(){    
    function onBefore() { alert('before'); }    
    $('.slideshow').cycle({
        before: onBefore
    });
});

Or an anonymous function:

$(function(){
    $('.slideshow').cycle({
        before: function() { alert('before'); }
    });
});
Nick Craver
oh man, rookie mistake. can't believe i overlooked that. Thanks!!
neil