Skip to content

Instantly share code, notes, and snippets.

@carusog
Created March 1, 2013 17:22
Show Gist options
  • Save carusog/5066230 to your computer and use it in GitHub Desktop.
Save carusog/5066230 to your computer and use it in GitHub Desktop.
$.fn.toggleClick = function(){
var methods = arguments, // store the passed arguments for future reference
count = methods.length; // cache the number of methods
//use return this to maintain jQuery chainability
return this.each(function(i, item){
// for each element you bind to
var index = 0; // create a local counter for that element
$(item).click(function(){ // bind a click handler to that element
return methods[index++ % count].apply(this,arguments); // that when called will apply the 'index'th method to that element
// the index % count means that we constrain our iterator between 0 and (count-1)
});
});
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment