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.
On this page · 5 min · Beginner
On this page
- Learning objectives
- Explanation
- Syntax
- Code example
- Step by step
- Real-world use
- Common mistakes
- Best practices
- Remember this
- Practice exercise
- Mini quiz
- Related topics
- My notes
- Reading time
- 5 min
- Difficulty
- Beginner
- Updated
- Oct 28, 2026
Related tutorials
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#
$('#error').show();
$('#error').hide();
$('#menu').toggle();
$('#gst').toggle(isBusiness);
$('#menu').is(':visible');
Code example: Show extra fields and toggle a FAQ#
<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>
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”.
Step-by-step explanation#
- The GST row starts hidden with the hidden attribute, so it never flashes.
- .toggle(this.checked) shows the row only while the checkbox is ticked.
- The FAQ toggle flips visibility, then reads it back with is(":visible").
- 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#
Hidden elements are still in the HTML and visible in DevTools. Do not send data the user must not see.
Hiding with JavaScript after load shows the element briefly. Hide it in HTML/CSS first.
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?
Show solution
<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>
Mini quiz#
What CSS does .hide() apply?
Want more? Practise all jQuery MCQs · jQuery interview questions
My notes#
Saved in this browser.