jQuery .slideToggle() Method

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

What Is .slideToggle()?

.slideToggle() (since jQuery 1.0) displays or hides matched elements with a sliding motion by animating height. Visible elements slide up and get display: none; hidden elements slide down and show. Returns the jQuery object for chaining.

Remember
$("#panel").slideToggle();              // default 400 ms
$("#panel").slideToggle("slow");        // 600 ms
$("#panel").slideToggle(600, "linear", function () { /* done */ });

// Visible → slide up → display:none
// Hidden  → slide down → fully visible
// display is saved and restored (e.g. inline stays inline)
NeedUse
Animated height toggle$el.slideToggle()
Expand only.slideDown()
Collapse only.slideUp()
Opacity toggle.fadeToggle()
Instant toggle.toggle()

How to Use .slideToggle()

Select the element, call .slideToggle() from a click handler — jQuery picks the direction.

  1. Select the target with jQuery ($("#panel")).
  2. On a user action, call .slideToggle() (optional duration / easing / complete).
  3. jQuery checks visibility and runs slide-up or slide-down.
  4. On hide, height reaches 0 then display: none frees layout space.
  5. For spam clicks, chain .stop(true, true) before .slideToggle().
PieceRequired?Role
Matched setYesElements to slide open or closed
durationNoDefault 400; also "fast" / "slow"
easingNoSince 1.4.3 — swing (default) or linear
completeNoOnce per element when its slide ends
Return value—jQuery object (chainable)

Pseudocode

Pseudocode
call $(elements).slideToggle(duration?, easing?, complete?)

for each matched element:
    if element is visible:
        animate height → 0
        set display: none
    else:
        restore saved display
        animate height → natural height

    if complete was provided:
        call complete once for this element
        (this = the DOM element)

return the jQuery set for chaining

Cheat Sheet

Keep these facts handy while building slide toggles.

TopicDetail
Syntax.slideToggle([duration] [, easing] [, complete])
Options form.slideToggle({ duration, easing, complete, … })
SincejQuery 1.0 (easing arg since 1.4.3)
Default duration400 ms
Presets"fast" = 200 · "slow" = 600
Easingswing (default) · linear
DisplaySaved / restored; none when fully hidden
Rapid clicks$el.stop(true, true).slideToggle()

Live Preview

Click Run to slideToggle the sample panel. Change duration with the chips.

This panel slides open or closed with .slideToggle().

Each Run toggles visibility. Reset forces the panel visible again.

Result
Ready — click Run to slideToggle.

jQuery Examples

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

Example 1 — Slow Toggle on Click

Official pattern: element starts shown; each click slides it closed or open.

jQuery
$("#clickme").on("click", function () {
  $("#book").slideToggle("slow", function () {
    // Animation complete.
  });
});
Try It Yourself

How It Works

  1. Bind. The same handler runs on every click.
  2. Inspect. jQuery reads whether #book is visible.
  3. Animate. Visible → slide up; hidden → slide down.
  4. Callback. Fires once for that element when done.

Example 2 — Toggle All Paragraphs (Official Ex. 1)

One button slides every p up or down over 600 ms.

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

How It Works

  1. Set. Every matched p is toggled.
  2. Per element. Direction comes from each paragraph’s own visibility.
  3. Parallel. They animate together, not one after another.
  4. Preset. "slow" is 600 ms.

Example 3 — Toggle with Per-Element Counter

Official idea: toggle several panels; increment a counter in each complete callback.

jQuery
$("#aa").on("click", function () {
  $(".panel").slideToggle("slow", function () {
    var n = parseInt($("#count").text(), 10);
    $("#count").text(n + 1);
  });
});
Try It Yourself

How It Works

  1. Toggle set. All .panel elements animate.
  2. Complete. Runs once per panel, not once for the group.
  3. Count. Each finish increments the number.
  4. Tip. Use .promise().done() if you want one callback for all.

Example 4 — FAQ Accordion Header

Click a header to slide-toggle its answer — classic one-line accordion.

jQuery
$(".faq-header").on("click", function () {
  $(this).next(".answer").slideToggle("slow");
});
Try It Yourself

How It Works

  1. Sibling. .next(".answer") is the panel under the header.
  2. Start hidden. Answers begin with .hide().
  3. Toggle. One call opens or closes that answer.
  4. Optional. Close others with .slideUp() for one-open UX.

Example 5 — Linear Easing (Since 1.4.3)

Three-argument form: duration, easing, complete.

jQuery
$("#toggle-btn").on("click", function () {
  $("#drawer").slideToggle(600, "linear", function () {
    $(this).text("Toggle complete at constant speed");
  });
});
Try It Yourself

How It Works

  1. Duration. 600 ms total.
  2. Easing. "linear" keeps a constant rate.
  3. Direction. Still auto from visibility.
  4. Complete. Updates text when the tween ends.

Edge Cases & Pitfalls

Watch these when toggles feel wrong or “stack.”

queue

Rapid clicks queue animations

Use .stop(true, true) before .slideToggle() on toggle buttons users can spam.

callback

Complete runs once per element

Toggling three panels fires the callback three times. Use .promise().done() for one “all finished” hook.

display

Hide ends at display: none

The element leaves the layout when fully collapsed — that is intended.

fx.off

jQuery.fx.off skips the tween

When true, toggles snap open/closed instantly (duration 0).

vs toggle

.toggle() is not a slide

.toggle() shows/hides immediately. Use .slideToggle() for height animation.

Browser Support

.slideToggle() has been part of jQuery since 1.0 (positional easing since 1.4.3). It works in every browser that runs your loaded jQuery version — no separate browser feature flag.

✓ Stable · jQuery 1.0+

jQuery .slideToggle()

Supported across jQuery 1.x, 2.x, and 3.x. Prefer a current jQuery 3.x build. Height animation uses jQuery’s fx engine.

100% With jQuery
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
.slideToggle() Universal

Bottom line: Safe for FAQs, menus, and panels. Guard rapid clicks with .stop(true, true), and respect jQuery.fx.off for reduced motion.

Performance Notes

ApproachCostBest for
One panel slideToggleCheapAccordions, menus, help boxes
Many elements at onceMore layout workPrefer one wrapper when possible
Queued spam clicksWasted framesAlways .stop(true, true) on toggles

Height toggles trigger layout. One container is cheaper than sliding dozens of siblings — and stopping before toggle avoids stacked open/close queues.

Key Takeaways

  • Role: animated show/hide via height — since jQuery 1.0.
  • Call: $el.slideToggle() — default 400 ms; optional easing + complete.
  • Direction: from current visibility — no separate toggle flag.
  • Guard: .stop(true, true) before toggle on busy buttons.

One line: .slideToggle() slides an element open or closed from its current visibility — one call for both directions.

Frequently Asked Questions

.slideToggle() shows or hides matched elements with a sliding motion by animating height. If the element is visible, it slides up and hides; if hidden, it slides down and shows. One call replaces a manual slideDown / slideUp branch.
1) .slideToggle([duration] [, complete]) — optional duration and completion callback. 2) .slideToggle(options) — an options object with duration, easing, queue, step, progress, and Promise-style callbacks. 3) .slideToggle([duration] [, easing] [, complete]) — positional easing since jQuery 1.4.3.
.slideDown() always reveals; .slideUp() always collapses. .fadeToggle() toggles opacity, not height. .toggle() switches display instantly with no animation. .slideToggle() picks slideDown or slideUp from current visibility.
Milliseconds, or the strings "fast" (200 ms) and "slow" (600 ms). Default is 400 ms when omitted.
Once per matched element when that element’s slide finishes — not once for the whole set. Use .promise().done() when you need a single callback after every element completes.
Yes. jQuery saves and restores display as needed. If an element was inline before hiding, it returns to inline after showing. When height reaches 0 after a hide, display is set to none so the element no longer affects layout.

Next: .stop() Method

Stop or jump the current animation — essential for rapid-click slide toggles.

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

10 people found this page helpful