views:

48

answers:

3

Hi all,

Is there a way I can call the second function in toggle(function(){},function(){})

Thanks in advance

A: 

how about

toggle(function(){ callFunctionOne(); callFunctionTwo(); });
Jamiec
A: 

I suggest such solution:

toggle(function(){},secondFunction);

function secondFunction() {}

And now you can call it how you want :)

Thinker
+1  A: 

You could skip the toggle to the next function, if you really wanted to I suppose, for example:

$("div").toggle(function() {
    alert("First");
}, function() {
    alert("Second");
})​;

Then you can manually advance the .toggle() function array (without calling the first function), like this:

$("div").data('lastToggle' + $("div").data().events.click[0].handler.guid, 1);

Then just click the element or $("div").click(), etc, and it'll fire the second function.

You can give it a try here, note that this only works in jQuery 1.4+ and is really just to show it's possible. You should however either bind them in reverse order, or give the second argument a named function as the other answers suggest.

Nick Craver
@nick Hey nick, can u please explain your code ?
Ninja Dude
@Avinash - If you look at how `.toggle()` works internally it's much easier to understand: http://github.com/jquery/jquery/blob/master/src/event.js#L904 All we're doing it setting a data variable it's looking for when the `click` event happens. The `click` isn't bound to *any* of the functions you pass, rather it's bound to another wrapper function that gets/runs the function it's currently on (via `.apply()`). We're just setting the start position it stores before the first `click` event happens, so it finds it and executes the second function on the first pass.
Nick Craver