1

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. :)

2
  • Odd and Even from a table rows/list items? Commented Nov 11, 2011 at 16:45
  • Thanks everyone! :D I just assumed toggle hid elements. :"> Commented Nov 11, 2011 at 23:23

3 Answers 3

4

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.

Sign up to request clarification or add additional context in comments.

Comments

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

1 Comment

Just to save precious CPU cycles: var flag=true; $(".a").click (function () {if (flag) FunctionB();flag=!flag;}); $(".a").click (function () {if (!flag) FunctionB();flag=!flag;});
0

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

3 Comments

The information in the body of this answer is right, but the link is not helpful. That's for effect toggle not event toggle.
That's where I copied/pasted from. Scroll down and you'll see "Toggle Between Functions" It doesn't talk about the effect anymore. I just showed you my source
My apologies! It's there after all. sheepish

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.