jQuery · The DOM

Hide and Show

Showing and hiding parts of a page — a menu, an error, an advanced options panel — is one of the most common UI tasks. jQuery does it in one call.

Beginner 5 min Lesson 8 of 22 Runnable examplesExamples use jQuery 3.x.
Before you start:CSS Manipulation
0/22 in jQuery
On this page · 5 min · Beginner

Learning objectives#

  • Use show(), hide() and toggle()
  • Toggle based on a condition
  • Check visibility with :visible / is(":visible")
  • Keep hidden content accessible

Explanation#

.hide() sets display:none; .show() restores the element’s normal display. .toggle() flips between them.

.toggle(condition) shows when true and hides when false — ideal for “show the GST field only for business customers”.

$(el).is(":visible") tells you whether an element currently takes up space.

Pass a duration to animate: .hide(300) or .show("fast"). (Next lesson covers effects in depth.)

For content that should start hidden, set it in CSS or HTML (hidden attribute or a class) so it never flashes on page load. Update aria-expanded on the toggle button for screen readers.

Syntax#

JavaScript Illustrative
$('#error').show();
$('#error').hide();
$('#menu').toggle();
$('#gst').toggle(isBusiness);
$('#menu').is(':visible');

Code example: Show extra fields and toggle a FAQ#

HTMLShow extra fields and toggle a FAQ Runnable
<script src="https://code.jquery.com/jquery-3.7.1.min.js"></script>
<div style="font-family:sans-serif">
  <label><input type="checkbox" id="business"> Buying for a business</label>
  <p id="gstRow" hidden>GST number: <input></p>


  <button id="faqBtn" aria-expanded="false" aria-controls="faq">Show FAQ</button>
  <div id="faq" style="display:none">Delivery takes 3–5 days.</div>
</div>
<script>
  $(function () {
    $('#business').on('change', function () {
      $('#gstRow').prop('hidden', false).toggle(this.checked);
    });


    $('#faqBtn').on('click', function () {
      $('#faq').toggle();
      const open = $('#faq').is(':visible');
      $(this).text(open ? 'Hide FAQ' : 'Show FAQ').attr('aria-expanded', open);
      console.log('FAQ visible:', open);
    });
  });
</script>
Expected output
Ticking “Buying for a business” shows the GST field; unticking hides it. The FAQ button shows and hides the answer, changes its own label, updates aria-expanded, and logs “FAQ visible: true/false”.
Opens this example in the full playground

Step-by-step explanation#

  1. The GST row starts hidden with the hidden attribute, so it never flashes.
  2. .toggle(this.checked) shows the row only while the checkbox is ticked.
  3. The FAQ toggle flips visibility, then reads it back with is(":visible").
  4. The button text and aria-expanded reflect the state for all users.

Real-world use case#

A registration form shows the “Company name” and “GST number” fields only when “Business account” is selected, and hides the error banner once the user fixes the input.

Common mistakes#

Hiding sensitive data with CSS only

Hidden elements are still in the HTML and visible in DevTools. Do not send data the user must not see.

Content flashing on load

Hiding with JavaScript after load shows the element briefly. Hide it in HTML/CSS first.

Forgetting accessibility

A toggle without aria-expanded gives screen-reader users no clue the content changed.

Best practices#

  • Start hidden in markup, reveal with JS
  • Use toggle(condition) for state
  • Update aria-expanded on toggle buttons
  • Prefer classes for complex show/hide styling

Remember this#

hide = display:none. show = back to normal. toggle = flip.

toggle(true/false) = show/hide by condition.

Hidden ≠ secret.

Practice exercise#

Your task: Show a "Password too short" message (#hint) while the password input has fewer than 8 characters, and hide it otherwise.

Need a hint?
On input: $("#hint").toggle(this.value.length < 8).
Show solution
HTML Runnable
<script src="https://code.jquery.com/jquery-3.7.1.min.js"></script>
<input type="password" id="pw"> <span id="hint" style="color:red;display:none">Password too short</span>
<script>
  $(function () {
    $('#pw').on('input', function () {
      $('#hint').toggle(this.value.length > 0 && this.value.length < 8);
    });
  });
</script>
Opens this example in the full playground

Mini quiz#

Hide and Show quizMultiple choice Question 1 of 3

What CSS does .hide() apply?

Score so far: 0/0

Want more? Practise all jQuery MCQs · jQuery interview questions

My notes#

Saved in this browser.

Progress is saved in this browser. Sign in to sync.