jQuery Hide and Show
The simplest jQuery effects make elements disappear and reappear. Without arguments they are instant; with a duration they animate width, height and opacity together.
hide(), show() and toggle()
| Method | What it does |
|---|---|
.hide() |
Hides the element (sets display: none) |
.show() |
Shows it again, restoring its original display value |
.toggle() |
Hides visible elements, shows hidden ones |
.toggle(bool) |
true → show, false → hide |
All three accept the same optional arguments:
$(selector).hide(duration, easing, callback);| Argument | Values |
|---|---|
duration |
Milliseconds (400) or "fast" (200 ms) / "slow" (600 ms). Default without argument: instant |
easing |
"swing" (default, slow-fast-slow) or "linear". Plugins such as jQuery UI add more |
callback |
A function that runs when the animation finishes |
<!DOCTYPE html>
<html>
<head>
<style>
body { font-family: sans-serif; }
#card { width: 220px; padding: 14px; background: #0769ad; color: #fff; border-radius: 8px; margin-top: 10px; }
</style>
</head>
<body>
<button id="hide">Hide</button>
<button id="show">Show</button>
<button id="toggle">Toggle (slow)</button>
<div id="card">I am a card. Hide me, show me, toggle me.</div>
<script src="https://code.jquery.com/jquery-3.7.1.min.js"></script>
<script>
$("#hide").on("click", function () {
$("#card").hide(); // instant
});
$("#show").on("click", function () {
$("#card").show(400); // 400 ms animation
});
$("#toggle").on("click", function () {
$("#card").toggle("slow", function () {
console.log("visible now?", $(this).is(":visible"));
});
});
</script>
</body>
</html>Notice how an animated hide shrinks the box from its top-left corner while fading — width, height, padding, margin and opacity all animate to zero.
How hiding works
.hide()stores the element's currentdisplayvalue and then setsdisplay: none. The element no longer takes up space, and the content below moves up..show()restores the stored value (block,flex,inline, …). If there is none, it uses the element's default display for its tag.visibility: hiddenandopacity: 0are not the same as.hide(): those elements are invisible but still take up space, and jQuery counts them as:visible.
| Check | Returns true when |
|---|---|
$el.is(":visible") |
The element takes up space in the layout |
$el.is(":hidden") |
It is display: none, inside a hidden parent, or has no size |
Show and hide in a real UI
A typical use is a "Show more" panel. The button label changes depending on the new state:
<!DOCTYPE html>
<html>
<head>
<style>
body { font-family: sans-serif; max-width: 360px; }
.details { display: none; background: #f4f4f4; padding: 8px 12px; border-radius: 6px; }
button { margin: 6px 0; }
</style>
</head>
<body>
<h3>Wireless Headphones</h3>
<p>Comfortable, 30-hour battery, foldable.</p>
<button class="more">Show details</button>
<div class="details">
<ul>
<li>Bluetooth 5.3</li>
<li>Weight: 250 g</li>
<li>USB-C fast charging</li>
</ul>
</div>
<script src="https://code.jquery.com/jquery-3.7.1.min.js"></script>
<script>
$(".more").on("click", function () {
const $btn = $(this);
$btn.next(".details").toggle(200, function () {
const open = $(this).is(":visible");
$btn.text(open ? "Hide details" : "Show details");
});
});
</script>
</body>
</html>The .details element starts hidden through CSS. .toggle() reads the current state, so you do not need your own isOpen variable.
Hiding many elements at once
Because jQuery methods apply to every matched element, you can filter a list with one line per action:
<!DOCTYPE html>
<html>
<head>
<style>
body { font-family: sans-serif; }
li { padding: 2px 0; }
</style>
</head>
<body>
<button data-filter="all">All</button>
<button data-filter="fruit">Fruit</button>
<button data-filter="veg">Vegetables</button>
<ul id="food">
<li class="fruit">🍎 Apple</li>
<li class="veg">🥕 Carrot</li>
<li class="fruit">🍌 Banana</li>
<li class="veg">🥦 Broccoli</li>
<li class="fruit">🍇 Grapes</li>
</ul>
<script src="https://code.jquery.com/jquery-3.7.1.min.js"></script>
<script>
$("button").on("click", function () {
const kind = $(this).data("filter");
const $items = $("#food li");
if (kind === "all") {
$items.show("fast");
} else {
$items.not("." + kind).hide("fast");
$items.filter("." + kind).show("fast");
}
console.log("filter:", kind);
});
</script>
</body>
</html>Tip:
.toggle(condition)replaces an if/else:$items.each(function () { $(this).toggle($(this).hasClass(kind)); }).
Note:
.toggle(fn1, fn2)— the old "alternate between two click handlers" form — was removed in jQuery 1.9. Today.toggle()only shows and hides.
Test yourself
What CSS does .hide() ultimately set on the element?
visibility: hiddenopacity: 0display: noneheight: 0
How long does .show("slow") take?
- 200 ms
- 400 ms
- 600 ms
- 1000 ms
What does $(".tip").toggle(false) do?
- Toggles the visibility
- Hides all
.tipelements - Shows all
.tipelements - Removes the toggle handler