jQuery Callback function won't take name of function(), must have an anonymous function
Asked Answered
C

2

5

This works:

$("#formbottom").slideUp(speed,'swing',function(){
    openSubmitting();
});

This doesn't:

$("#formbottom").slideUp(speed,'swing',
    openSubmitting()
);

When you have a callback, do you always have to have an anonymous function in there? Can't you just put the function you want to call?

Caliper answered 11/1, 2013 at 1:58 Comment(0)
E
10

openSubmitting() calls your function. You don't want the result of your function. You want your actual function, which is why you write function() {...} instead of (function() {...})().

Since you want to pass a reference to your function, remove those parentheses:

$("#formbottom").slideUp(speed,'swing',
    openSubmitting
);
Experientialism answered 11/1, 2013 at 1:59 Comment(1)
Ahh... yes, thanks. Works without quotes, for future searchers. I guess that's because js is considering openSubmitting as like a variable name, not a string like php: call_user_func('function'). If anyone has a link to eg 'jQuery Eye for the Php Guy'... lol, that would be helpful!Caliper
N
6

Have you tried jQuery("#formbottom").slideUp(speed,'swing',openSubmitting);?

Nakada answered 11/1, 2013 at 1:59 Comment(2)
Thanks for the correct answer, Blender gets the check for the good explanation though.Caliper
It's alright, I don't mind actually. Just want to help :) Besides Blender deserves it because he explained it well.Nakada

© 2022 - 2024 — McMap. All rights reserved.