jQuery – Multiple Toggle Event Extension
November 18th, 2007
I’ve hacked a quick jQuery extension to allow you to overload the toggle method to allow more than 2 toggle events. It’s not exactly rocket science but I need to be able to give an element three toggle states and the built in version only allows two.
Simple state extra toggles separated by commas like normal:
jQuery.fn.extend({
toggle: function() {
var a = arguments;
return this.click(function(e) {
if (this.lastToggle == undefined || this.lastToggle+1 >= a.length) {
this.lastToggle = 0;
} else {
this.lastToggle++;
}
// Make sure that clicks stop
e.preventDefault();
// and execute the function
return a[this.lastToggle].apply( this, [e] ) || false;
});
}
});
UPDATE: Multiple toggle states are now supported natively in jQuery 1.2.6.
November 20th, 2007 at 8:14 am
*ahem*