Why does this jquery slideToggle code not work?

Posted by cbmeeks on Stack Overflow See other posts from Stack Overflow or by cbmeeks
Published on 2010-05-17T13:30:15Z Indexed on 2010/05/17 14:10 UTC
Read the original article Hit count: 517

Filed under:
|

I am trying to alter the button text for a expand/collapse button. Basically, a user clicks "Collapse" and I perform a slideToggle and when the slideToggle is done, I change the button text to "Expand" and vice-versa.

The following code works great however if you click the expand/collapse button rapidly, it looses its mind and shows "Expand" when it's already expanded or "Collapse" when it's already collapsed.

Any tips are appreciated.

Thanks!

function toggleBox( button, box ){ if($(box).is(":hidden")) { $(box).slideToggle("slow", function(){ $(button).html("Collapse"); }); } else { $(box).slideToggle("slow", function(){ $(button).html("Expand"); }); } }

© Stack Overflow or respective owner

Related posts about jQuery

Related posts about slidetoggle