jQuery .slideToggle(): Toggle a Collapsible Panel

jQuery .slideToggle() expands a hidden element and collapses a visible one. It animates the height, so content below moves during the effect. It suits a help panel controlled by a single disclosure button.

Switch between showing and hiding an element

Duration is measured in milliseconds and defaults to 400. 'fast' is 200 ms and 'slow' is 600 ms. The default swing easing changes speed during the animation, while linear moves steadily. Pass an options object or positional arguments such as .slideToggle(400, 'swing', complete). See the official jQuery .slideToggle() documentation.

$("#panel").slideToggle({ duration: 400, easing: "linear" });

The method starts or queues the animation and returns a jQuery object immediately. Use its completion callback to run work afterward. A regular callback function runs once per selected element with this set to that element.

Try a panel with toggle and reset controls

Save the entire code as jquery-slidetoggle-example.html in UTF-8 and open it in a browser. It needs an internet connection to load the full jQuery 4.1.0 build. The effects module is included in this build. See the jQuery full and slim build descriptions.

<!doctype html>
<html lang="en">
<head>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <title>Toggle a panel with jQuery .slideToggle()</title>
  <script src="https://code.jquery.com/jquery-4.1.0.min.js"></script>
  <style>
    body { margin: 1rem; color: #1b2b36; font: 1rem/1.6 system-ui, sans-serif; }
    main { max-width: 36rem; margin-inline: auto; }
    button { font: inherit; }
    #panel { box-sizing: border-box; margin-block: .7rem; padding: .85rem;
      background: #e6f2fa; border: 1px solid #50748c; }
    #panel p { margin-top: 0; }
    :focus-visible { outline: 3px solid #005d90; outline-offset: 3px; }
  </style>
</head>
<body>
  <main>
    <h1>Toggle a panel with jQuery .slideToggle()</h1>
    <button id="toggle" type="button" aria-controls="panel" aria-expanded="false">Expand</button>
    <div id="panel" style="display: none" inert>
      <p>This help content appears while the panel is expanded.</p>
      <button id="close" type="button">Close panel</button>
    </div>
    <button id="reset" type="button">Reset</button>
    <p id="status" aria-live="polite">Collapsed</p>
  </main>
  <script>
    $(function () {
      const $button = $("#toggle");
      const $panel = $("#panel");
      const panel = $panel[0];
      const motion = matchMedia("(prefers-reduced-motion: reduce)");
      let expanded = false;
      let revision = 0;

      function setExpanded(next) {
        if (next === expanded) return;
        const request = ++revision;
        expanded = next;
        if (!next && panel.contains(document.activeElement)) $button[0].focus();

        $panel.stop(true, true);
        panel.inert = !next;
        $button.attr("aria-expanded", String(next));
        $button.text(next ? "Collapse" : "Expand");

        const duration = motion.matches ? 0 : 400;
        $panel.slideToggle(duration);

        $panel.promise("fx").done(function () {
          if (request === revision) $("#status").text(next ? "Expanded" : "Collapsed");
        });
      }

      $button.on("click", () => setExpanded(!expanded));
      $("#close").on("click", () => setExpanded(false));
      $("#reset").on("click", function () {
        revision += 1;
        expanded = false;
        if (panel.contains(document.activeElement)) $button[0].focus();
        $panel.stop(true, true).hide();
        panel.inert = true;
        $button.attr("aria-expanded", "false").text("Expand");
        $("#status").text("Collapsed");
      });
    });
  </script>
</body>
</html>

The panel starts hidden. Select Expand to open it and see the completion status. Select the outside button again or use Close panel inside. Reset finishes any active effect, clears waiting effects, and hides the panel immediately. With reduced motion enabled, toggling is immediate too.

Handle rapid input and keyboard focus

When the panel closes while an inner control has focus, the example moves focus to the outside control. It applies inert while closing and while closed so its inner control stays out of the Tab order. The outside button uses aria-expanded to expose the state. See the WAI disclosure pattern for the keyboard pattern.

The example calls .stop(true, true) first. It clears waiting effects and jumps the active effect to its end, giving .slideToggle() a settled state to reverse. Earlier completion work can run during that step, so the example changes its status text only for the latest request number. See the .stop() documentation.

Run one action after every panel has toggled

Use the completion callback for per-element work. Use .promise('fx').done(...) for one action after all effects on the selection end. Here, fx is the default effects queue. See the .promise() documentation.

const $panels = $(".batch-panel");

$panels.slideToggle(400, "swing", function () {
  this.dataset.finished = "true";
}).promise("fx").done(function () {
  $("#batch-count").text($panels.length);
});

Give two elements the batch-panel class. Hide the first with style="display: none" and leave the second visible. Add an output with id="batch-count". The first panel opens and the second closes. Each receives data-finished="true", and the output displays 2 after both finish.

Diagnose an effect that does not run

The demo starts with inline display: none. If the element has a hidden attribute, remove it when you intend to reveal the element. Check for display: none !important and inspect the ancestors. Changing a child’s display cannot show it inside a hidden parent. In the Network panel, also check that you loaded the full jQuery build with effects.

The effect reverses each selected element’s visibility independently. A mixed selection can remain mixed afterward. Use .slideUp() to collapse content or .slideDown() to expand content when a server response or another part of the UI specifies the desired end state.