The click-handler form of .toggle() bound alternating functions to the click event — first click runs handler one, second click runs handler two, then the cycle repeats. This tutorial covers .toggle(fn1, fn2) since 1.0, three-or-more handler cycling, manual replication with .on("click"), and why the API was removed in jQuery 1.9.
01
.toggle(a,b)
Alternate
02
3+ handlers
Cycle all
03
click
Under hood
04
.on()
Modern
05
.toggleClass()
Prefer
06
Removed
In 1.9
Fundamentals
Introduction
Before jQuery 1.9, you could bind two click handlers in one call: $("#target").toggle(fn1, fn2). The first function ran on odd clicks, the second on even clicks — perfect for simple on/off toggles without writing state management by hand. jQuery also provided a separate .toggle() for showing and hiding elements — same method name, different job.
The click-handler form was deprecated in 1.8 and removed in 1.9 because it was fragile: applying it twice to one element failed, cleanup required .off("click"), and the implementation called preventDefault() so links would not navigate. Modern jQuery expects explicit .on("click") handlers or .toggleClass() for selection styling.
Concept
Understanding the .toggle() Click-Handler Method
The official jQuery API describes the click-handler .toggle() as a way to bind two or more handlers executed on alternate clicks. Internally it attaches to the click event — the same rules as .on("click") apply. Each matched element keeps its own click counter so handlers cycle independently.
With two handlers, clicks alternate: 1st → handler A, 2nd → handler B, 3rd → handler A again. With three handlers, the cycle is A → B → C → A. The method returns the jQuery object for chaining.
⚠️
Removed in jQuery 1.9
Do not use .toggle(fn1, fn2) in jQuery 1.9+ or 3.x — it no longer exists. Replicate with .on("click") and a counter, or use .toggleClass() for class toggling. Do not confuse this with animation .toggle() for show/hide, which still works in jQuery 3.x.
Foundation
📝 Syntax
The click-handler .toggle() had one signature from the official jQuery API. It was deprecated in 1.8 and removed in 1.9:
There is no "toggle" event — legacy .toggle(fn, fn) registered on click.
.off("click") removes all click handlers, including those from .toggle().
4. Not the same as animation .toggle()
jQuery
$( "#panel" ).toggle(); // show/hide — still in jQuery 3.x
$( "#panel" ).toggle( true ); // show
$( "#panel" ).toggle( false ); // hide
Return value
Click-handler .toggle() returned the jQuery object for chaining (in jQuery 1.8 and earlier).
Cheat Sheet
⚡ Quick Reference
Goal
Legacy (.toggle click)
Modern replacement
Alternate two click handlers
$("#t").toggle(fn1, fn2) ⚠
$("#t").on("click", fn) with counter
Cycle three handlers
$("#t").toggle(fn1, fn2, fn3) ⚠
index % 3 inside one click handler
Toggle selected class on cells
$("td").toggleClass("selected") — prefer over legacy .toggle(fn, fn)
Remove legacy toggle handlers
$("#t").off("click") — no separate "toggle" event
Show/hide elements
$("#panel").toggle() — animation API, still in jQuery 3.x
Compare
📋 Click .toggle(fn, fn) vs animation .toggle() vs .toggleClass() vs .on("click")
Four APIs share the name toggle — know which one alternates click handlers and which toggles visibility or classes.
.toggle(fn, fn)
removed
Legacy alternating click handlers — removed in jQuery 1.9
.toggle()
show/hide
Animation visibility toggle — still available in jQuery 3.x
.toggleClass()
class
Recommended for selected/highlight states on click
.on("click")
modern
Explicit click handler with counter or state — use for new code
Hands-On
Examples Gallery
Examples 1–3 follow the official jQuery API documentation. Examples 4–5 cover manual replication and .toggleClass(). Try-it labs 1–3 use jQuery 1.12.4 because the click-handler .toggle() was removed in 1.9. Remember: do not use this API in jQuery 3.x.
📚 Official Alternating Click Handlers
Legacy jQuery demos — handlers cycle on each click.
Example 1 — Official Demo: Two Handlers on #target
Alternate alert messages on each click — the canonical .toggle(fn1, fn2) pattern from the jQuery API.
1st click → "First handler for .toggle() called."
2nd click → "Second handler for .toggle() called."
3rd click → first handler again — cycle repeats
How It Works
jQuery maintains an internal click counter per element. Odd clicks run the first function; even clicks run the second. The method binds to the click event internally and calls preventDefault() — links will not navigate while this handler is active.
Example 2 — Three Handlers Cycling
Pass three functions — jQuery cycles through all of them: 1st click → fn1, 2nd → fn2, 3rd → fn3, 4th → fn1 again.
With more than two handlers, jQuery uses modulo arithmetic on the click count. The official API notes that with three handlers, the first runs on clicks 1, 4, 7, and so on.
Example 3 — Official Demo: Toggle selected Class on Table Cells
Legacy pattern from the API docs — add class on first click, remove on second. The API notes: not recommended; use .toggleClass() instead.
jQuery
$( "td" ).toggle(
function() {
$( this ).addClass( "selected" );
},
function() {
$( this ).removeClass( "selected" );
}
);
1st click on td → .selected added
2nd click on same td → .selected removed
Official API recommends .toggleClass("selected") instead
How It Works
This was a common misuse of click-handler .toggle() — using two handlers to add and remove a class. .toggleClass("selected") on a single click handler is simpler and works in jQuery 3.x.
📈 Modern Replacements
Replicate alternating behavior or use better APIs in jQuery 3.x.
Example 4 — Manual Replication with .on("click") and a Counter
Same alternating behavior as .toggle(fn1, fn2) — works in jQuery 3.x.
Click 1 → handler A message
Click 2 → handler B message
Click 3 → handler A again
Use count % N for N handlers
How It Works
The official API notes that the same behavior can be implemented by hand when .toggle(fn, fn) limitations matter. A module-level or element-data counter replaces jQuery’s internal rotation. For three handlers, use count % 3.
Example 5 — Prefer .toggleClass() for Selection States
Modern replacement for the official table-cell demo — one click toggles the class.
Each click on td → .selected toggles on/off
Works in jQuery 3.x — no alternating handler pair needed
How It Works
When the goal is simply on/off class state per click, .toggleClass() is the right tool — not click-handler .toggle(). See the jQuery CSS .toggleClass() tutorial for more patterns.
Applications
🚀 Common Use Cases
On/off toggles — alternate two click actions without a state variable — legacy .toggle(fnOn, fnOff).
Multi-state UI — cycle three or more views with .toggle(fn1, fn2, fn3).
Table cell selection — add/remove selected class — better done with .toggleClass() today.
Reading legacy code — recognize click-handler .toggle() in jQuery 1.8 and earlier codebases.
Manual migration — replace with .on("click") + counter when upgrading past jQuery 1.8.
Avoid confusion — distinguish from animation .toggle() for show/hide panels.
🧠 How Click-Handler .toggle() Cycled Handlers
1
You call .toggle(fn1, fn2)
jQuery stores the handler list and binds internally to the click event on each matched element.
bind
2
Odd clicks
1st, 3rd, 5th click — run the first handler. jQuery increments an internal counter per element.
handler A
3
Even clicks
2nd, 4th, 6th click — run the second handler. With three handlers, cycles A → B → C → A.
handler B
4
👉
preventDefault called
Links do not navigate and buttons may not submit — unbind with .off("click"), not .off("toggle").
Important
📝 Notes
Removed in jQuery 1.9 — not available in jQuery 3.x. Use .on("click") with a counter or .toggleClass().
Deprecated in jQuery 1.8; removed in 1.9.
Binds to the click event internally — same rules as .on("click") apply.
There is no "toggle" event — unbind with .off("click").
Calling .toggle(fn, fn) twice on the same element was unreliable — avoid stacking.
Do not confuse click-handler .toggle(fn, fn) with animation .toggle() for show/hide.
For class toggling, prefer .toggleClass() over alternating add/remove handlers.
Compatibility
Browser Support
The click-handler .toggle(fn, fn) was a jQuery-only API available in jQuery 1.0 through 1.8. It was removed in 1.9 and is absent from jQuery 3.x. The underlying click event works in every browser — replicate alternating behavior with .on("click").
✓ Removed 1.9
jQuery .toggle() click handlers
Legacy API for jQuery 1.8 and earlier only. Animation .toggle() for visibility remains in jQuery 3.x. Modern replacement: .on('click') with a counter or .toggleClass() for class states.
N/ANot in jQuery 3.x
Google ChromeAll versions · Desktop & Mobile
Full support
Mozilla FirefoxAll versions · Desktop & Mobile
Full support
Apple SafariAll versions · macOS & iOS
Full support
Microsoft EdgeAll versions · Chromium & Legacy
Full support
Internet ExplorerIE 6+ · Legacy environments
Full support
OperaAll modern versions
Full support
.toggle(fn,fn)Removed
Bottom line: Do not use click-handler .toggle() in new code. When maintaining jQuery 1.8 apps, plan migration to .on('click') before upgrading to jQuery 3.x.
Wrap Up
Conclusion
The click-handler form of jQuery .toggle() bound alternating functions to the click event — odd clicks ran the first handler, even clicks the second, with support for three or more handlers in rotation. It was deprecated in 1.8 and removed in 1.9.
For jQuery 3.x, replicate with .on("click") and a counter, or use .toggleClass() when toggling CSS classes. Do not confuse this API with animation .toggle() for showing and hiding elements. When you encounter legacy .toggle(fn, fn) in old code, recognize it, understand the click rotation, and migrate before upgrading jQuery.
Use .on("click", fn) with an explicit counter or state for alternating actions
Use .toggleClass() when toggling CSS classes on click
Check arguments when reading .toggle() — functions mean click handlers, none means animation
Call .off("click") to remove legacy toggle handlers
Upgrade jQuery 1.8 apps by replacing .toggle(fn, fn) before moving to 3.x
❌ Don’t
Use click-handler .toggle(fn, fn) in jQuery 1.9+ or 3.x — it does not exist
Try to unbind with .off("toggle") — there is no toggle event
Apply .toggle(fn, fn) twice to the same element — unreliable in legacy jQuery
Confuse click-handler .toggle() with show/hide animation .toggle()
Expect links to navigate when click-handler .toggle() is bound — preventDefault blocks defaults
Summary
Key Takeaways
Knowledge Unlocked
Six things to remember about click .toggle()
Alternate, cycle, migrate.
6
Core concepts
A/B01
.toggle(a,b)
Alternate
Legacy
3+02
Cycle
3 handlers
Rotate
click03
click
Under hood
Event
class04
.toggleClass()
Prefer
Modern
.on05
.on("click")
Replicate
Counter
1.906
Removed
In 1.9
Gone
❓ Frequently Asked Questions
The click-handler form of .toggle() binds two or more functions to the click event on matched elements. On the first click, the first handler runs; on the second click, the second handler runs; then the cycle repeats. With three handlers, jQuery cycles first → second → third → first. This signature was deprecated in jQuery 1.8 and removed in jQuery 1.9 — it is not available in jQuery 3.x.
No. jQuery overloads the name .toggle(). With function arguments — .toggle(fn1, fn2) — it binds alternating click handlers. With no functions — .toggle(), .toggle(true), .toggle(false) — it shows or hides elements with animation. The click-handler form was removed in 1.9; the visibility animation method remains in jQuery 3.x. Always check the arguments when reading legacy code.
The API was deprecated in 1.8 because it had sharp edges: calling .toggle() twice on the same element was unreliable, unbinding required .off('click') not .off('toggle'), and the implementation called preventDefault() which blocked default link and button behavior. jQuery steers developers toward explicit .on('click') handlers with a counter or state variable instead.
Use a click counter: var n = 0; $('#target').on('click', function(){ if (n % 2 === 0) fn1.call(this); else fn2.call(this); n++; }); Or track explicit state with a boolean. For class toggling on table cells, prefer .toggleClass('selected') or .on('click', fn) with explicit add/remove logic.
Legacy .toggle(fn1, fn2) registered handlers on the click event internally — there is no separate 'toggle' event name. Remove them with .off('click') on the same selector. Be aware other click handlers on the same element may also be removed unless you pass the specific handler function to .off('click', fn).
No. The click-handler signature was removed in jQuery 1.9. jQuery 3.x only supports .toggle() for showing/hiding elements. Maintain legacy pages on jQuery 1.8.x if you must keep the old API temporarily, but migrate to .on('click') with a counter or .toggleClass() when upgrading.
Did you know?
jQuery overloaded the name .toggle(). When passed functions, it cycled click handlers — removed in 1.9. When passed no functions, it toggles element visibility — still in jQuery 3.x. Legacy click-handler .toggle() registered on the click event and called preventDefault(), which is why unbinding requires .off("click"), not .off("toggle").