jQuery .slideUp(): Collapse and Hide Content
jQuery .slideUp() animates an element’s vertical size to hide it. Once the effect finishes, display: none removes its layout space and the element remains in the document. Use a completion callback or wait for the effects queue before running follow-up code.
Collapse an element out of the layout
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 .slideUp(400, 'swing', complete). See the official jQuery .slideUp() documentation.
$("#panel").slideUp({ 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.
Run a collapsible panel example
Save the entire code as jquery-slideup-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>Collapse a panel with jQuery .slideUp()</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>Collapse a panel with jQuery .slideUp()</h1>
<button id="toggle" type="button" aria-controls="panel" aria-expanded="true">Collapse</button>
<div id="panel">
<p>You can also close this panel from the inside.</p>
<button id="close" type="button">Close panel</button>
</div>
<p id="status">Expanded</p>
</main>
<script>
$(function () {
const $button = $("#toggle");
const $panel = $("#panel");
const panel = $panel[0];
const motion = matchMedia("(prefers-reduced-motion: reduce)");
let expanded = true;
function setExpanded(next) {
if (next === expanded) return;
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;
if (next) $panel.slideDown(duration);
else $panel.slideUp(duration);
$panel.promise("fx").done(function () {
if (expanded === next) $("#status").text(next ? "Expanded" : "Collapsed");
});
}
$button.on("click", () => setExpanded(!expanded));
$("#close").on("click", () => setExpanded(false));
});
</script>
</body>
</html>
The panel starts expanded. Activate the outside Collapse button or the Close panel button inside it. The panel slides closed and the completion text updates. Use the outside button to expand it again. When the system requests reduced motion, the example uses zero duration.
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) before starting a new effect. The first true clears waiting effects, and the second finishes the active effect immediately. Its completion handler can run at that point, so the demo updates the message only if it matches the latest request. See the .stop() documentation for queue behavior.
Run an action once all elements have collapsed
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.slideUp(400, "swing", function () {
this.dataset.finished = "true";
}).promise("fx").done(function () {
$("#batch-count").text($panels.length);
});
To try the code above, add two elements with class batch-panel and an output element with id="batch-count". After both effects complete, each panel has data-finished="true" and the output reads 2.
Diagnose an effect that does not run
If you see slideUp is not a function, check the Network panel for the full build. Verify that your selector matches and that the element is currently visible. If other effects run before it, inspect the element’s fx queue.
Use .slideDown() documentation to reopen a closed panel, or .slideToggle() documentation to choose a sliding effect from its current visibility. In a real interface, update its button label, focus, and accessibility state together.




