jQuery .toggle() Method

Beginner
7 min read
Updated: Oct 2026
5 examples
Live preview

What Is .toggle()?

.toggle() shows or hides matched elements by flipping their current visibility (since jQuery 1.0). Instant with no args; with a duration it becomes an animation. Display is saved and restored like .hide() / .show().

Remember
$(".target").toggle()                 // instant flip
$("p").toggle("slow")                 // 600ms animation
$("#box").toggle(true)                // force show (1.3+)
$("#box").toggle(false)               // force hide (1.3+)
$("#box").toggle(400, function () {   // ms + complete
  // this = current element
});

// "fast" = 200ms · default animated duration = 400ms
NeedUse
Flip visibility$(el).toggle()
Always hide$(el).hide()
Always show$(el).show()
Force state$(el).toggle(true|false)
Fade flip only$(el).fadeToggle()

How to Use .toggle()

Select elements, then flip visibility instantly, with a duration, or to a forced Boolean state.

  1. Select the element(s) to toggle.
  2. Call .toggle() for an instant flip, or pass a duration / options.
  3. Or pass true / false to force show / hide (jQuery 1.3+).
  4. Optionally pass a complete callback (runs once per element).
PieceRequired?Role
Matched setYesElements to flip
durationOptionalms, "slow", or "fast"
displayOptionaltrue show · false hide (1.3+)
easingOptional"swing" or "linear" (1.4.3+)
completeOptionalRuns after each element finishes

Pseudocode

Pseudocode
for each matched element el:
    if argument is Boolean:
        if true  → show(el)   // same as .show()
        if false → hide(el)   // same as .hide()
        continue

    if el is currently visible:
        hide(el)              // save display → none
    else:
        show(el)              // restore saved display

    if duration provided:
        animate width, height, opacity during the flip
        call complete(this = el) once per element

return original matched set

Cheat Sheet

Keep these facts handy while flipping visibility.

TopicDetail
Instant.toggle()
Animated.toggle(duration[, easing][, complete])
Boolean.toggle(true) / .toggle(false) (1.3+)
SincejQuery 1.0 (Boolean 1.3 · easing 1.4.3)
Durations"fast" 200 · default 400 · "slow" 600
DisplaySaved and restored like hide / show
DOMElement stays; layout space comes and goes
Global offjQuery.fx.off = true

Live Preview

Flip the sample box instantly, slowly, or force show / hide.

Browser demo of .toggle() — same idea as the examples below.

Hello — I am the target box.

Result
$("#box").toggle()
→ flips current visibility

jQuery Examples

Short, copy-ready snippets. Open DevTools Console, or use Try It Yourself for a live editor.

Example 1 — Instant Toggle

Flip paragraph visibility immediately with no animation.

jQuery
$("p").toggle();
Try It Yourself

How It Works

  1. Select. $("p") matches every paragraph.
  2. Check. jQuery tests each element’s current visibility.
  3. Flip. Visible → hide; hidden → show (instant).
  4. DOM. Nodes remain; only display changes.

Example 2 — Toggle With "slow"

Animate the flip over about 600 milliseconds.

jQuery
$("p").toggle("slow");
Try It Yourself

How It Works

  1. Duration. "slow" means 600ms.
  2. Animate. Width, height, and opacity move during the flip.
  3. Finish. Final display is set for show or hide.
  4. Queue. Animated toggles join the effects queue (unlike duration 0).

Example 3 — Boolean Force Show / Hide

Pass true or false when you already know the desired state.

jQuery
var show = true;
$("p").toggle(show);   // same as .show()
show = false;
$("p").toggle(show);   // same as .hide()
Try It Yourself

How It Works

  1. Boolean. Since 1.3, a Boolean skips the visibility check.
  2. true. Calls the same path as .show().
  3. false. Calls the same path as .hide().
  4. Use case. Drive UI from a checkbox, flag, or condition.

Example 4 — Toggle, Then Log

Run code in the complete callback after the animation.

jQuery
$("#box").toggle(400, function () {
  console.log("done:", $(this).is(":visible"));
});
Try It Yourself

How It Works

  1. Animate. 400ms toggle runs first.
  2. Callback. Fires when that element finishes.
  3. this. Inside the function, this is the DOM element.
  4. Check. .is(":visible") reports the new state.

Example 5 — Toggle On Click

Classic pattern: a button flips all paragraphs each click.

jQuery
$("button").on("click", function () {
  $("p").toggle();
});
Try It Yourself

How It Works

  1. Bind. .on("click") listens on the button.
  2. Toggle. Each click flips every <p>.
  3. No if/else. You do not need separate show and hide branches.
  4. Pattern. Matches the official jQuery .toggle() click demo.

Edge Cases & Pitfalls

Check these before wiring toggle into real UIs.

name

Not the old event-handler .toggle()

Alternating click handlers were removed in 1.9. This method only flips visibility (or forces Boolean state).

queue

Instant toggle skips the queue

No duration (or 0) runs immediately and can override queued animations.

a11y

Update ARIA when toggling panels

Pair with aria-expanded / focus management on menus and dialogs so assistive tech stays in sync.

fx

jQuery.fx.off forces instant

When effects are off globally, animated .toggle() becomes duration 0 — plan for that in tests.

Browser Support

.toggle() has been part of jQuery since 1.0. Boolean display arrived in 1.3; the easing argument in 1.4.3. It works wherever your loaded jQuery version runs.

✓ Stable · jQuery 1.0+

jQuery .toggle()

Supported in all browsers that run jQuery 1.0+. Prefer a current jQuery 3.x build for modern projects.

100% jQuery core method
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() Universal

Bottom line: Safe for everyday UI. Prefer instant toggle for large sets; animate only when motion helps understanding.

Performance Notes

ApproachCostBest for
.toggle() instantCheapBulk flip, tables, lists
.toggle("slow")Layout + paintSingle panels, feedback
Many animated togglesExpensiveAvoid — toggle one container instead

Instant toggle is fine at scale. Animated toggle on dozens of nodes can jank — animate one container instead of every child.

Key Takeaways

  • Role: flip visibility (or force show/hide with a Boolean).
  • Pair: built on the same display cache as .hide() / .show().
  • Animate: pass duration / "slow" / "fast".
  • Not events: the old click-handler .toggle() is gone since 1.9.

One line: .toggle() is the jQuery one-liner for on/off visibility without writing show/hide branches.

Frequently Asked Questions

.toggle() flips visibility of matched elements. Visible ones are hidden; hidden ones are shown. With no arguments the flip is instant. jQuery saves and restores display like .hide() and .show(). Returns the jQuery collection for chaining. Available since jQuery 1.0.
.show() always reveals; .hide() always hides. .toggle() checks current visibility and does the opposite — one call for on/off buttons, menus, and panels.
Since jQuery 1.3, a Boolean forces state: .toggle(true) shows (like .show()), .toggle(false) hides (like .hide()). Useful when you already know the desired state.
Yes. Pass a duration in ms, or "slow" (600) / "fast" (200). jQuery animates width, height, and opacity while flipping visibility. You can also pass easing, an options object, or a complete callback.
No. jQuery also had .toggle(handlerA, handlerB) for alternating click handlers — removed in jQuery 1.9. This page covers visibility .toggle(). For alternating handlers, use .on("click") with your own state flag.
Set jQuery.fx.off = true. All effects, including .toggle(), run with zero duration. Useful for tests or reduced motion.
Once per matched element when that element’s animation finishes — not once for the whole set.

Next: jQuery.ready

Learn when the DOM is ready so your toggle code runs at the right time.

jQuery.ready 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.

8 people found this page helpful