bobber205
bobber205

Reputation: 13372

jQuery Toggle -- One Function One Click Then Another

Here's what I would like to do in pseudocode form.

Bind Two Functions to Click Event

On 1st/Odd Clicks Do FunctionA()

On 2nd/Even Clicks Do FunctionB()

The .toggle() function seems to not be designed for this.

I'm sure this exists in jQuery but my googling hasn't resulted in anything. :)

Upvotes: 1

Views: 1239

Answers (3)

lonesomeday
lonesomeday

Reputation: 237817

I don't see how this isn't exactly what toggle is designed for...

$('#someEl').toggle(FunctionA, FunctionB);

You may be confused by the other toggle function that exists in jQuery, which won't be called if you pass functions as the arguments.

Upvotes: 4

user85569
user85569

Reputation: 384

You just need to ask Google the right question ;)

$(selector).toggle(function(),function(),function(),...)
  • function() Required. Specifies a function to run every EVEN time the element is clicked
  • function() Required. Specifies a function to run every ODD time the element is clicked
  • function(),... Optional. Specifies additional functions to toggle between

http://www.w3schools.com/jquery/eff_toggle.asp

Upvotes: 0

Royi Namir
Royi Namir

Reputation: 148514

var i=1;
$(".a").click (function () {if (i%2!=0) FunctionA();i++;});
$(".a").click (function () {if (i%2==0) FunctionB();i++;});

Upvotes: 1

Related Questions