dc

jQuery 1.9+ event toggler plugin

With the release of jQuery 1.9 using .toggle() as .toggle(handler(eventObject),handler(eventObject)) was deprecated and removed. Last year one of my clients needed to use this functionality as they had other triggers happening at the same time as the content being shown or hidden. To assist with the upgrade I wrote a plugin for jQuery called ToggleEvent that does something similar to the old .toggle() syntax. Just recently I upgraded another one of my work projects to jQuery 1.11.0 and I forgot about the loss of .toggle() being used this way. Luckily I remembered that I had solved the problem before and dusted off the ToggleEvent plugin.

Realizing that I had forgotten to both write about the plugin, and publish it, I have now done so at github: jQuery ToggleEvent plugin

It works similarly to the old syntax of .toggle()

<div id="target">
	Click here
</div>
$( "#target" ).toggleEvent(
	'click',
	function() {
		alert( "First handler for .toggle() called." );
	},
	function() {
		alert( "Second handler for .toggle() called." );
	}
);

As the element is clicked repeatedly, the messages alternate:

First handler for .toggleEvent() called.
Second handler for .toggleEvent() called.
First handler for .toggleEvent() called.
Second handler for .toggleEvent() called.
First handler for .toggleEvent() called.

It will also handle cycling through any additional handlers that are passed in the same way as the old .toggle() did.

Leave a reply