Henry Yun
Henry Yun

Reputation: 544

alternate between two functions on click

How do I alternate between calling two functions on click? For example, here is the button

<div id="fooButton" />

and here is the javascript

function fooOne() {
     alert('foo One called');
}

function fooTwo() {
     alert('foo Two called');
}

how do I call fooOne() and fooTwo() on alternate button clicks?

Upvotes: 3

Views: 4257

Answers (2)

ThiefMaster
ThiefMaster

Reputation: 318518

You can use the toggle pseudo-event:

$('#fooButton').toggle(fooOne, fooTwo);

Note: This is deprecated as of jQuery 1.8 and removed in jQuery 1.9+. You can use jQuery-migrate if you need this function anyway.

Upvotes: 11

Kevin Ennis
Kevin Ennis

Reputation: 14466

Use jQuery's toggle() method

$("#fooButton").toggle(fooOne, fooTwo);

Edited to make use of your named functions rather than sloppily using anonymous ones.

Upvotes: 6

Related Questions