Legacy .toggle(handler, handler [, handler…]) bound alternating click handlers (since 1.0). First click ran handler 1, second ran handler 2, then the cycle repeated. Removed in jQuery 1.9.
Remember
// Legacy (≤1.8 only — removed in 1.9)
$("#box").toggle(fn1, fn2)
$("#box").toggle(fn1, fn2, fn3)
// Modern replacements:
var n = 0;
$("#box").on("click", function () {
(n++ % 2 === 0 ? fn1 : fn2).call(this);
});
$("#cell").on("click", function () {
$(this).toggleClass("selected");
});
Read old code safely, then rewrite for jQuery 3.x.
Identify .toggle(fn, fn) — functions mean click cycling, not show/hide.
Count how many handlers cycle (2, 3, or more).
Replace with .on("click") + a counter, or .toggleClass() for class flips.
Unbind leftovers with .off("click") (there is no "toggle" event).
Piece
Required?
Role
handler (1st)
Yes
Runs on clicks 1, 3, 5… (with 2 handlers)
handler (2nd)
Yes
Runs on clicks 2, 4, 6…
Extra handlers
Optional
Cycle continues through all of them
Return value
—
jQuery set (chainable)
Pseudocode
Pseudocode
function toggle(element, handlers[]):
// removed API — for reading legacy code only
i = 0
on click(element):
handlers[i](event)
i = (i + 1) % handlers.length
preventDefault() // blocked links / buttons
// Modern:
on click(element):
handlers[n % length](event)
n++
Reference
Cheat Sheet
Keep the migration map next to legacy files.
Topic
Detail
Signature
.toggle(fn, fn [, fn…])
Added
jQuery 1.0
Deprecated
1.8
Removed
1.9 (gone in 3.x)
Under the hood
Bound a click handler
Unbind
.off("click")
Side effect
Called preventDefault()
Try it
Live Preview
Modern jQuery 3.x demos — equivalent behavior without the removed API.
Uses .on("click") / .toggleClass() — same ideas as Examples 4–5. Legacy labs 1–3 need jQuery 1.8.x.
Result
Click Run to simulate alternating handlers.
Code
jQuery Examples
Examples 1–3 show the removed API (labs use jQuery 1.8.3). Examples 4–5 are the jQuery 3.x replacements.
Example 1 — Two Alternating Handlers (Legacy)
Official style: .toggle(fn1, fn2) — works only on jQuery ≤1.8.
jQuery
$("#target").toggle(
function () {
console.log("First handler for .toggle() called.");
},
function () {
console.log("Second handler for .toggle() called.");
}
);
Links did not navigate and some buttons behaved oddly. Modern counters do not force that.
unbind
No "toggle" event name
Cleanup used .off("click"), which could wipe other click handlers on the same element.
twice
Applying twice was unreliable
Calling .toggle(fn…) twice on one element was not guaranteed to work. Prefer one explicit .on("click").
Compatibility
Browser Support
Click-handler .toggle(fn, fn) existed only in jQuery 1.0–1.8. It was removed in 1.9 and is absent from jQuery 3.x. The underlying click event works everywhere — replicate with .on("click").
✓ Removed 1.9+
.toggle(fn, fn)
Legacy-only API. Use on/click or toggleClass in every modern browser with jQuery 3.x.
0%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 ship click-handler .toggle(). Migrate to .on("click") + counter or .toggleClass().
Analysis
Performance Notes
Approach
Cost
Note
Legacy .toggle(fn…)
1 click handler
Unavailable in 3.x
.on("click") + counter
Same
Clear and portable
.toggleClass()
Cheapest for CSS
Best for on/off styles
A small counter is as cheap as the old helper — and you keep default click behavior under your control.
Remember
Key Takeaways
Removed:.toggle(fn, fn) since jQuery 1.9 — not in 3.x.
Replace:.on("click") + counter for alternating logic.
Classes: prefer .toggleClass() over add/remove pairs.
Name clash: visibility .toggle() still exists — different arguments.
One line: treat .toggle(fn, fn) as historical — ship .on("click") or .toggleClass() instead.
Frequently Asked Questions
It bound two or more click handlers that ran on alternate clicks: first click → first handler, second → second, then the cycle repeats. Deprecated in 1.8 and removed in 1.9 — not available in jQuery 3.x.
No. With function arguments it was alternating click handlers (removed). With no functions — .toggle(), .toggle(true/false), or a duration — it shows/hides elements and still exists in jQuery 3.x.
It was fragile: applying it twice was unreliable, cleanup needed .off("click") (not "toggle"), and it called preventDefault() so links/buttons lost default behavior. Prefer .on("click") with a counter or .toggleClass().
Use a click counter: var n = 0; $("#t").on("click", function () { if (n % 2 === 0) fn1.call(this); else fn2.call(this); n++; }); For class flips, prefer .toggleClass("selected").
They registered on click — use .off("click"). There is no "toggle" event name. Other click handlers on the same element may be removed unless you pass a specific function to .off.
No. That signature is gone. Only visibility .toggle() remains. Read legacy code on jQuery ≤1.8, then migrate to .on("click") or .toggleClass().