jQuery .toggle() Method — Alternating Click Handlers

Beginner
5 min read
Updated: Nov 2026
3 examples
Mouse Events

Definition

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");
});
CallWhat 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

ParameterDescription
Step 1Identify .toggle(fn, fn) — functions mean click cycling, not show/hide.
Step 2Count how many handlers cycle (2, 3, or more).
Step 3Replace with .on("click") + a counter, or .toggleClass() for class flips.

Return Value

Return ValueDescription
jQuery objectMost 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.");
  }
);
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.

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.

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.

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

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

dblclick Event — Handle Double-Clicks tutorial →

CODETOFUN NEWS

Stay Ahead in Tech

Get the latest IT news, programming updates, and technology trends directly on your phone.

Your daily dose of tech, straight to your feed.

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