jQuery .toggle() Method

Beginner
5 min read
Updated: Nov 2026
3 examples
DOM

Definition

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

Syntax

Start with these common calls. Keep them short and readable.

jQuery
$(".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
CallWhat it does
.toggle()Learn jQuery .toggle(): flip element visibility instantly or with animation, boolean show/hide since 1.3, toggle vs show vs hide, durations, callbacks, examples, and FAQs.

Load jQuery before your script (CDN or local file), then wrap DOM work in $(function () { ... }) when needed.

Parameters

ParameterDescription
Step 1Select the element(s) to toggle.
Step 2Call .toggle() for an instant flip, or pass a duration / options.
Step 3Or pass true / false to force show / hide (jQuery 1.3+).

Return Value

Return ValueDescription
BooleanOften true or false depending on the match or state.
jQuery objectMost setters and traversing helpers return the jQuery collection so you can chain more methods.

jQuery Examples

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

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.

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

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: ready Property — DOM Ready Thenable

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

ready Property — DOM Ready Thenable 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.

8 people found this page helpful