jQuery .toggle() Method — Alternating Click Handlers

Beginner
Removed in 1.9
6 min read
Updated: Oct 2026
5 examples
Live preview

What Is Click-Handler .toggle()?

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");
});
NeedLegacyModern
Alternate 2 handlers.toggle(fn1, fn2).on("click", …) + counter
Cycle 3+ handlers.toggle(fn1, fn2, fn3)Counter % handlers.length
Flip a CSS class.toggle(add, remove).toggleClass()
Show / hideDOM .toggle() (still valid)

How to Use (Legacy) & Migrate

Read old code safely, then rewrite for jQuery 3.x.

  1. Identify .toggle(fn, fn) — functions mean click cycling, not show/hide.
  2. Count how many handlers cycle (2, 3, or more).
  3. Replace with .on("click") + a counter, or .toggleClass() for class flips.
  4. Unbind leftovers with .off("click") (there is no "toggle" event).
PieceRequired?Role
handler (1st)YesRuns on clicks 1, 3, 5… (with 2 handlers)
handler (2nd)YesRuns on clicks 2, 4, 6…
Extra handlersOptionalCycle 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++

Cheat Sheet

Keep the migration map next to legacy files.

TopicDetail
Signature.toggle(fn, fn [, fn…])
AddedjQuery 1.0
Deprecated1.8
Removed1.9 (gone in 3.x)
Under the hoodBound a click handler
Unbind.off("click")
Side effectCalled preventDefault()

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.

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.");
  }
);
Try It Yourself

How It Works

  1. Two functions are registered as a click cycle.
  2. Odd clicks (1st, 3rd…) run the first handler.
  3. Even clicks run the second handler.
  4. Removed in 1.9 — do not use in jQuery 3.x.

Example 2 — Three Handlers (Legacy)

With three callbacks, clicks cycle A → B → C → A…

jQuery
$("#cycle").toggle(
  function () { console.log("Handler 1"); },
  function () { console.log("Handler 2"); },
  function () { console.log("Handler 3"); }
);
Try It Yourself

How It Works

  1. Click 1, 4, 7… → handler 1.
  2. Click 2, 5, 8… → handler 2.
  3. Click 3, 6, 9… → handler 3.
  4. Index wraps with modulo over the handler list.

Example 3 — Table Cells (Legacy)

Official demo idea: add a class on first click, remove on second. Prefer .toggleClass().

jQuery
$("td").toggle(
  function () {
    $(this).addClass("selected");
  },
  function () {
    $(this).removeClass("selected");
  }
);
Try It Yourself

How It Works

  1. First click on a cell adds selected.
  2. Second click on that cell removes it.
  3. Each cell keeps its own click count internally.
  4. Modern: $("td").on("click", function () { $(this).toggleClass("selected"); }).

Example 4 — Modern Counter (jQuery 3.x)

Same alternating behavior without the removed API.

jQuery
var n = 0;
$("#target").on("click", function () {
  if (n % 2 === 0) {
    console.log("Handler A — 1st, 3rd, 5th…");
  } else {
    console.log("Handler B — 2nd, 4th, 6th…");
  }
  n++;
});
Try It Yourself

How It Works

  1. n tracks how many clicks happened.
  2. Even n runs handler A; odd n runs handler B.
  3. Increment after each click.
  4. Works on jQuery 3.x with no removed APIs.

Example 5 — Prefer .toggleClass()

For on/off styling, one method replaces the old add/remove pair.

jQuery
$("td").on("click", function () {
  $(this).toggleClass("selected");
  console.log($(this).text() + ": " +
    ($(this).hasClass("selected") ? "on" : "off"));
});
Try It Yourself

How It Works

  1. Bind click on each td (or use delegation).
  2. toggleClass flips selected each click.
  3. hasClass reports the new state for logging.
  4. No counter needed for simple on/off UI.

Edge Cases & Pitfalls

Why the old API was removed — and traps when migrating.

removed

Gone in jQuery 1.9+

.toggle(fn, fn) throws or does nothing useful in 3.x. Always migrate before upgrading.

overload

Name clash with show/hide

Check arguments: functions → old click cycle; duration/boolean → visibility .toggle().

default

Called preventDefault()

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").

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 Chrome All versions · Desktop & Mobile
Full support
Mozilla Firefox All versions · Desktop & Mobile
Full support
Apple Safari All versions · macOS & iOS
Full support
Microsoft Edge All versions · Chromium & Legacy
Full support
Internet Explorer IE 6+ · Legacy environments
Full support
Opera All modern versions
Full support
.toggle(fn, fn) Removed

Bottom line: Do not ship click-handler .toggle(). Migrate to .on("click") + counter or .toggleClass().

Performance Notes

ApproachCostNote
Legacy .toggle(fn…)1 click handlerUnavailable in 3.x
.on("click") + counterSameClear and portable
.toggleClass()Cheapest for CSSBest for on/off styles

A small counter is as cheap as the old helper — and you keep default click behavior under your control.

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().

Next: dblclick event

Handle double-clicks with the modern .on("dblclick") API.

dblclick tutorial →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

9 people found this page helpful