The .toggle() method flips visibility on matched elements — show if hidden, hide if visible. This tutorial covers instant toggling since jQuery 1.0, the Boolean form since 1.3, animated toggles with "slow" and "fast", complete callbacks, comparisons with .show() and .hide(), the deprecated event-handler overload, the official jQuery API demos, and five practical try-it examples.
01
Flip
Show/hide
02
Boolean
Since 1.3
03
Animated
Duration
04
vs show/hide
One API
05
Saves display
Cache
06
Since 1.0
Core API
Fundamentals
Introduction
Many UI controls need a single action that switches between two states: open a menu or close it, expand a panel or collapse it, show details or hide them. Instead of checking visibility and calling .show() or .hide() manually, jQuery provides .toggle().
With no parameters, .toggle() inspects each matched element. If it is visible, jQuery hides it; if hidden, jQuery shows it. The flip is instant and jQuery saves the original display value the same way .hide() and .show() do.
Since jQuery 1.3 you can pass true or false to force show or hide without flipping. Pass a duration for animated toggles. Pair with .hide() and .show() when you always know the direction. Browse the jQuery DOM hub for related methods.
Concept
Understanding the .toggle() Method
.toggle( [duration ] [, complete ] ) operates on a jQuery collection. Each matched element is flipped independently; the method returns the same collection for chaining. Hidden elements become visible; visible elements become hidden.
Important: jQuery also had a different .toggle( handlerA, handlerB ) overload for alternating click handlers. That event version was deprecated in jQuery 1.8 and removed in jQuery 1.9. Modern code uses .on( "click", fn ) with a state variable instead. This tutorial covers the visibility.toggle() only.
💡
Beginner Tip
The official jQuery demo binds a button click to $( "p" ).toggle(). Each click hides visible paragraphs and shows hidden ones — perfect for simple show/hide panels without writing if/else logic.
Foundation
📝 Syntax
jQuery .toggle() supports several forms:
Instant toggle — since 1.0
jQuery
.toggle()
No arguments — flip visibility immediately with no animation.
Visible elements hide; hidden elements show.
Boolean toggle — since 1.3
jQuery
.toggle( display )
.toggle( true ) // same as .show()
.toggle( false ) // same as .hide()
Animated toggle with duration
jQuery
.toggle( duration [, complete ] )
.toggle( "slow" ) // 600 ms
.toggle( "fast" ) // 200 ms
.toggle( 400 ) // 400 ms
📋 .toggle() vs .show() vs .hide() vs .toggleClass()
Four related visibility tools — pick the right one for your UI.
.toggle()
flip
Show hidden elements, hide visible ones — one call for on/off controls
.show()
visible
Always reveal — use when you know the target state is shown
.hide()
hidden
Always hide — use when you know the target state is hidden
.toggleClass()
CSS class
Flip a class name — pair with CSS for visibility when !important rules block effects
Hands-On
Examples Gallery
Example 1 follows the official jQuery API documentation. Examples 2–5 cover animated toggles, Boolean forcing, image flip with callback, and a practical accordion menu. Use DevTools or the Try-it links to run each snippet in the browser.
📚 Getting Started
Official jQuery demo and instant flip fundamentals.
Example 1 — Official Demo: Toggle All Paragraphs
Click a button to flip every paragraph between visible and hidden.
Click "Toggle": visible paragraphs hide, hidden ones show
Second click: state flips back
Both "Hello" and "Good Bye" toggle together
Official jQuery API Example 1 pattern
How It Works
jQuery checks each element’s current visibility. The same button click alternates between hide and show without you writing conditional logic.
Example 2 — Animated Toggle with "slow"
Flip paragraph visibility over 600 milliseconds with a smooth animation.
Click "Toggle 'em": visible paragraphs shrink/fade out (600 ms)
Hidden paragraphs grow/fade in (600 ms)
Direction depends on current state
Official jQuery API Example 2 pattern
How It Works
When a duration is provided, .toggle() delegates to the same animation engine as .show() and .hide(). Width, height, and opacity animate together during the flip.
📈 Boolean & Practical Patterns
Forced states, callbacks, and menu toggles.
Example 3 — Boolean Toggle (Show All, Then Hide All)
Use .toggle( true ) and .toggle( false ) to force show or hide based on a counter.
jQuery
var flip = 0;
$( "button" ).on( "click", function() {
$( "p" ).toggle( flip++ % 2 === 0 );
} );
// Even clicks: .toggle(true) → show all
// Odd clicks: .toggle(false) → hide all
Click 1: flip=0 → toggle(true) → all paragraphs shown
Click 2: flip=1 → toggle(false) → all paragraphs hidden
Click 3: toggle(true) again …
Official jQuery API Example 3 pattern — since 1.3
How It Works
The Boolean form bypasses the automatic flip. Pass true when your logic already decided the element should be visible; pass false to hide regardless of current state.
Example 4 — Toggle Image with Complete Callback
Flip a visible image slowly; the callback runs when each animation finishes.
First click: #book hides slowly (was visible)
Second click: #book shows slowly (was hidden)
Callback fires after each element's animation completes
Official jQuery book demo pattern
How It Works
Animated .toggle() accepts the same callback as .show() and .hide(). Inside the function, this is the DOM element that just finished animating.
Example 5 — Accordion Panel Toggle
Click a heading to toggle its content panel — a common real-world pattern.
jQuery
$( ".accordion-heading" ).on( "click", function() {
$( this ).next( ".accordion-panel" ).toggle( "fast" );
} );
// Each heading toggles only its own panel
// "fast" = 200 ms animation
Click "Section A": panel A toggles open/closed (200 ms)
Click "Section B": panel B toggles independently
Panels start hidden in CSS (display: none)
Practical accordion without a plugin
How It Works
Scope the toggle to $( this ).next( ".accordion-panel" ) so each section flips independently. Combine with .hide() on sibling panels if you want only one open at a time.
Applications
🚀 Common Use Cases
Toggle buttons — one click shows or hides a panel without if/else.
Accordion menus — expand and collapse FAQ sections or settings groups.
Mobile nav — hamburger icon toggles the navigation drawer.
Details disclosure — “Read more” links flip extra content.
Forced state — use .toggle(true/false) when logic already knows the target visibility.
Animated flip — pass "slow" or a millisecond value for smooth transitions.
🧠 How .toggle() Flips Visibility
1
Match target elements
jQuery collection identifies which nodes to flip.
Elements
2
Check current visibility
Visible → call hide logic. Hidden → call show logic. Boolean arg skips this step.
State
3
Flip instantly or animate
No duration → immediate. With duration → animate size and opacity during the flip.
Effect
4
✓
Return jQuery collection
Same matched set returned for chaining — display saved/restored like hide/show.
Important
📝 Notes
Available since jQuery 1.0; Boolean form since 1.3; easing since 1.4.3.
No-argument form flips instantly — visible hides, hidden shows.
"slow" = 600 ms, "fast" = 200 ms; numeric default when animating is 400 ms.
Event-handler .toggle(fn, fn) was removed in jQuery 1.9 — not the same as visibility toggle.
Callback runs once per matched element after its animation completes.
Does not override display: none !important — use .toggleClass() instead.
Compatibility
Browser Support
.toggle() has been part of jQuery since 1.0+ (Boolean form since 1.3+). It uses standard CSS display and jQuery's built-in effects engine. Works in all browsers supported by your jQuery build — IE6+ through modern evergreen browsers in legacy jQuery versions, and all current browsers in jQuery 3.x and 4.x.
✓ jQuery 1.0+
jQuery .toggle()
Universal visibility flip API across jQuery 1.x, 2.x, 3.x, and 4.x. Pair with .show() and .hide() when direction is known; use .toggle() for one-button on/off UIs.
100%With jQuery loaded
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()Universal
Bottom line: Default choice when a single control should flip visibility without if/else logic.
Wrap Up
Conclusion
The jQuery .toggle() method flips visibility on matched elements. Call it with no arguments for an instant show/hide flip, pass true or false to force a state since jQuery 1.3, or supply a duration and callback for animated toggles.
It combines the behavior of .show() and .hide() in one call — ideal for buttons, menus, and accordions. Next up: wait for the DOM with .ready() before moving on to the jQuery.ready thenable.
Use .toggle() for single-button show/hide controls
Use .toggle(true/false) when your code already knows the target state
Scope toggles with $(this).next() for accordion sections
Pair animated toggles with meaningful button labels (Open/Close)
Update aria-expanded for accessibility when toggling panels
❌ Don’t
Confuse visibility .toggle() with removed event-handler .toggle(fn, fn)
Use .toggle() when you always want show — call .show() directly
Animate hundreds of elements simultaneously without profiling performance
Expect .toggle() to override display: none !important in CSS
Forget mobile users — test instant vs animated toggles on slow devices
Summary
Key Takeaways
Knowledge Unlocked
Six things to remember about .toggle()
The one-call flip between visible and hidden.
6
Core concepts
⇄01
Flip
Show/hide
Toggle
T/F02
Boolean
Since 1.3
Force
🎬03
Animated
slow/fast
Fade
↔04
vs show/hide
One API
Pair
💾05
Saves display
Cache
Layout
→06
Chain
jQuery
Return
❓ Frequently Asked Questions
.toggle() flips the visibility of matched elements. If visible, it hides them; if hidden, it shows them. With no arguments the flip is instant. jQuery saves and restores display values like .hide() and .show(). Returns the jQuery collection for chaining. Available since jQuery 1.0.
.show() always reveals elements; .hide() always hides them. .toggle() checks current visibility and does the opposite — one method for on/off buttons, menus, and accordions instead of writing if/else with show and hide.
Since jQuery 1.3, pass a Boolean: .toggle(true) shows elements (same as .show()), .toggle(false) hides them (same as .hide()). Useful when you already know the desired state from a variable or condition.
Yes. Pass a duration in milliseconds, or 'slow' (600ms) or 'fast' (200ms). jQuery animates width, height, and opacity while flipping visibility. You can also pass an options object or complete callback. With no duration, the toggle is instant.
No. jQuery also had .toggle(handlerA, handlerB) for alternating click handlers — removed in jQuery 1.9. The visibility .toggle() documented here flips display. For alternating handlers, use .click() or .on('click') with your own state variable.
Set jQuery.fx.off = true globally. All jQuery effects, including .toggle(), then run with zero duration — effectively instant. Useful in tests or when motion must be reduced for accessibility. Reset to false to re-enable animations.
Did you know?
jQuery used to overload .toggle() with two meanings: visibility flip and alternating click handlers via .toggle( handlerEven, handlerOdd ). That event version was deprecated in 1.8 and removed in 1.9. If you see old tutorials calling .toggle(fn, fn), replace them with .on( "click", fn ) and a boolean state variable. The visibility .toggle() documented here is alive and well in jQuery 3.x and 4.x.