Skip to content
Tutorials Center
My learning

Topics

Lesson 24 of 56 ยท Effects and Animation

Showing and Hiding

Use jQuery .show(), .hide() and .toggle() with speeds, callbacks and the display property, compare them to CSS classes, and keep hidden content accessible.

Hiding and revealing things is the most common visual change on the web: menus, help text, error messages, filters, "read more" blocks. jQuery wraps this in three methods, .show(), .hide() and .toggle(), that work instantly or with a smooth animation, and that remember how an element was displayed so they can restore it correctly.

This lesson shows how they behave, how to control timing and callbacks, when a CSS class is the better tool, and what happens to hidden content for people who use assistive technology. It builds on selecting elements and events.

Three methods

.show() Makes matched elements visible.
.hide() Sets display to none.
.toggle() Flips between the two.
1 / 2

The basics

Without arguments the methods change visibility at once.

JavaScript
$("#help").hide();   // display: none
$("#help").show();   // restores the display value it had
$("#help").toggle(); // hide if visible, show if hidden

.hide() stores the element's current display value (such as block or flex) before setting display: none. .show() puts that value back, which is why it works correctly for a <div> (block), a <span> (inline) and a flex container alike. An element that your stylesheet hides with display: none has nothing to restore, so .show() sets it to the browser default for that tag.

You can pass a boolean to .toggle() to force a direction, which is handy when the state comes from a condition:

JavaScript
$("#details").toggle(checkbox.checked);

Speeds and durations

Give .show(), .hide() or .toggle() a duration and they animate. The duration is either a number of milliseconds or one of two keywords.

Value Milliseconds
"fast" 200
"slow" 600
a number exactly that many
"_default" or any unknown string 400

During the animation jQuery shrinks or grows the element's width, height, margins, padding and opacity together, and it sets display: none only when hiding finishes.

JavaScript
$("#banner").hide("slow");
$("#banner").show(250);

Completion callbacks

The last argument can be a function that runs when the animation finishes. Use it for work that must wait until the element is really hidden or shown.

JavaScript
$("#notice").hide(300, function () {
  $(this).remove(); // safe to remove now
});

Inside the callback this is the DOM element that finished. If your collection holds several elements, the callback runs once for each, not once for the whole set.

Speeds, toggling and callbacks

Click "toggle(1000)" twice quickly. The animations queue, which can feel sluggish. You will learn how to tame that in The Animation Queue.

Hide, remove, and visibility are different things

.hide() only sets display: none. The element stays in the DOM, keeps its data and event handlers, and still counts in your selectors ($("li") still finds it; $("li:visible") does not). Compare with the related options:

  • .remove() deletes the element from the page, handlers and data included. See Removing and Replacing.
  • CSS visibility: hidden hides the element but keeps its space in the layout, and jQuery's .hide() does not do that.
  • CSS opacity: 0 makes the element transparent but still clickable and in the layout.

Show and hide with classes instead

.show() and .hide() write inline style="display: ...", which is hard to override from a stylesheet and mixes behavior into presentation. For many cases a class is cleaner:

CSS
.is-hidden { display: none; }
JavaScript
$("#help").toggleClass("is-hidden");

Classes can also drive CSS transitions and respect prefers-reduced-motion in your stylesheet. A good rule: use classes for state your design system owns, such as an open menu; use .show(), .hide() and .toggle() for quick, self-contained reveals or when you want jQuery's size animation. The next lessons, Fading and Sliding and Custom Animations, build on the same engine.

Accessibility of hidden content

display: none removes an element from the accessibility tree: screen readers skip it and keyboard focus cannot reach it. That is exactly right for content that should be unavailable, but it matters when you build a reveal widget.

  • Keep the button that controls the reveal focusable, and mirror its state with aria-expanded="true|false".
  • Link the button to the panel with aria-controls.
  • Do not hide content that people need to read only to show it with a delay.
  • If the revealed text is an important status message, put it in a region with role="status" or aria-live="polite" so it is announced.

Animations also matter. Some users ask their system to reduce motion. jQuery can switch every animation off at once by setting jQuery.fx.off = true, and you can tie that to the user's setting:

JavaScript
jQuery.fx.off = window.matchMedia("(prefers-reduced-motion: reduce)").matches;

With effects off, .show() and .hide() still work, they just complete instantly.

An accessible disclosure

The page starts with the native hidden attribute so there is no flash before the script runs. The script then switches to jQuery-controlled hiding, and the button keeps aria-expanded and its label in sync.

Check your understanding

Quick check ยท 5 questions

of 5 answered You scored / 5 ยท perfect!

  1. 1. What does .hide() do to an element?

    It stores the old display value and sets display: none. The element stays in the DOM with its handlers and data, and .show() restores the stored value.
  2. 2. What is the duration of .show("slow")?

    "slow" is 600 ms and "fast" is 200 ms. The default duration of other animation calls is 400 ms.
  3. 3. A selection holds three elements and you pass a callback to .hide(300, fn). How often does fn run?

    jQuery calls the callback for each element in the collection, with this set to that element.
  4. 4. Why might a CSS class be better than .hide() for an open/closed menu?

    Inline styles written by .hide() are harder to override and keep design decisions in script. A class leaves them in CSS.
  5. 5. Which pair of attributes helps screen reader users with a show/hide button?

    aria-expanded announces whether the panel is open, and aria-controls links the button to the panel.

Summary

  • .show(), .hide() and .toggle() change display, restoring the element's previous value on show.
  • Add a duration ("fast" 200 ms, "slow" 600 ms, or milliseconds) to animate size and opacity.
  • A callback runs when each element finishes; this is that element.
  • Hidden elements stay in the DOM, unlike .remove().
  • Prefer CSS classes for design-owned state; use these methods for quick reveals.
  • Mirror state with aria-expanded, and respect reduced motion with jQuery.fx.off.

Next, explore the ready-made fade and slide effects in Fading and Sliding.