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.
Syntax
Start with these common calls. Keep them short and readable.
jQuery
// 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");
});
Call
What it does
.toggle()
Learn the removed jQuery .toggle(fn, fn) API: alternating click handlers, why it was removed in 1.9, and how to replace it with .on(\
Load jQuery before your script (CDN or local file), then wrap DOM work in $(function () { ... }) when needed.
Parameters
Parameter
Description
Step 1
Identify .toggle(fn, fn) — functions mean click cycling, not show/hide.
Step 2
Count how many handlers cycle (2, 3, or more).
Step 3
Replace with .on("click") + a counter, or .toggleClass() for class flips.
Return Value
Return Value
Description
jQuery object
Most setters and traversing helpers return the jQuery collection so you can chain more methods.
jQuery Examples
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.");
}
);
1st click on A1 → selected
2nd click on A1 → not selected
How It Works
First click on a cell adds selected.
Second click on that cell removes it.
Each cell keeps its own click count internally.
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().