jQuery .slideToggle() Method

Beginner
5 min read
Updated: Nov 2026
3 examples
Effects

Definition

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

Syntax

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

jQuery
$("#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)
CallWhat it does
.slideToggle()Learn jQuery .slideToggle(): show or hide matched elements with a sliding height animation since 1.0, duration presets, easing since 1.4.3, per-element callbacks, and compare with slideDown, slideUp, fadeToggle, and .toggle().

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

Parameters

ParameterDescription
Step 1Select the target with jQuery ($("#panel")).
Step 2On a user action, call .slideToggle() (optional duration / easing / complete).
Step 3jQuery checks visibility and runs slide-up or slide-down.

Return Value

Return ValueDescription
Value / undefinedGetters usually return a value from the first matched element, or undefined when nothing applies.

jQuery Examples

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.

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.

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.

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

10 people found this page helpful