chchrist
chchrist

Reputation: 19812

Trigger second function in toggle()

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

Upvotes: 1

Views: 5708

Answers (3)

Nick Craver
Nick Craver

Reputation: 630409

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.

Upvotes: 5

Thinker
Thinker

Reputation: 14464

I suggest such solution:

toggle(function(){},secondFunction);

function secondFunction() {}

And now you can call it how you want :)

Upvotes: 0

Jamiec
Jamiec

Reputation: 136104

how about

toggle(function(){ callFunctionOne(); callFunctionTwo(); });

Upvotes: 0

Related Questions