Question
I want to understand how to control an element's visibility in jQuery using .hide(), .show(), and .toggle(). I also want to know how to check whether an element is currently visible or hidden.
Short Answer
By the end of this page, you will know how jQuery hides and shows elements, how to toggle visibility, and how to test whether an element is visible or hidden using selectors like :visible and :hidden and methods like .is(). You will also see practical examples, common mistakes, and how this pattern appears in real projects.
Concept
In jQuery, visibility control means changing whether an element is displayed on the page.
The most common methods are:
.hide()— hides the element.show()— shows the element.toggle()— switches between hidden and visible
To check visibility, jQuery commonly uses:
$(selector).is(':visible')$(selector).is(':hidden')
These checks are useful when your code needs to decide what to do next. For example:
- open or close a dropdown
- show validation messages only when needed
- reveal extra form fields
- switch text like Show details / Hide details
Why this matters in real programming:
- User interfaces often react to clicks and state changes.
- Your code needs a reliable way to know what the user currently sees.
- Visibility checks help you avoid duplicated UI logic and make interactions predictable.
A simple example:
if ($('#panel').is(':hidden')) {
$('#panel').show();
}
This says: if the panel is currently hidden, show it.
In jQuery, visibility usually depends on whether the element takes up space in the layout. In practice, and are the most common tools for this kind of check.
Mental Model
Think of a web page like a stage.
.show()puts an actor back on stage..hide()removes the actor from the stage..toggle()checks whether the actor is on stage and flips the state.is(':visible')asks, "Can the audience currently see this actor?"is(':hidden')asks, "Is this actor currently off stage?"
So visibility checks are like looking at the stage before deciding what the next action should be.
Syntax and Examples
Core syntax
$('#box').hide();
$('#box').show();
$('#box').toggle();
$('#box').is(':visible');
$('#box').is(':hidden');
Example: toggle a message
$('#toggleButton').on('click', function () {
$('#message').toggle();
});
This switches #message between visible and hidden every time the button is clicked.
Example: check before acting
if ($('#message').is(':visible')) {
console.log('The message is visible');
} else {
console.log('The message is hidden');
}
Step by Step Execution
Traceable example
$('#toggleButton').on('click', function () {
if ($('#panel').is(':hidden')) {
$('#panel').show();
} else {
$('#panel').hide();
}
});
What happens step by step
- jQuery finds the element with id
toggleButton. - It attaches a click event handler to that button.
- When the user clicks the button, the function runs.
- jQuery checks
$('#panel').is(':hidden'). - If the panel is hidden:
$('#panel').show()runs- the panel becomes visible
- Otherwise:
$('#panel').hide()runs- the panel becomes hidden
Example state trace
Assume #panel starts hidden.
- First click:
is(':hidden')is → runs
Real World Use Cases
Common practical uses
Expand/collapse sections
FAQ items, settings panels, and help sections often hide content until the user asks to see it.
$('.faq-question').on('click', function () {
$(this).next('.faq-answer').toggle();
});
Form field logic
Show extra fields only if the user selects a certain option.
$('#hasCompany').on('change', function () {
if ($(this).is(':checked')) {
$('#companyFields').show();
} else {
$('#companyFields').hide();
}
});
Validation messages
Hide error messages until validation fails.
if ($('#email').val() === '') {
$().();
} {
$().();
}
Real Codebase Usage
In real projects, developers usually use visibility methods as part of UI state handling.
Common patterns
Guard clauses
Only show or hide when needed.
if ($('#modal').is(':visible')) {
return;
}
$('#modal').show();
This avoids unnecessary work or repeated UI actions.
Early returns
Stop logic when the current state already matches the goal.
function openSidebar() {
if ($('#sidebar').is(':visible')) return;
$('#sidebar').show();
}
Validation and feedback
Developers often combine visibility with error handling.
function validateName() {
if ($('#name').val().trim() === '') {
$().();
;
}
$().();
;
}
Common Mistakes
1. Checking the jQuery object itself instead of visibility
Broken code:
if ($('#box')) {
console.log('Visible');
}
Why this is wrong:
$('#box')returns a jQuery object.- That object can exist even if the element is hidden.
- This does not test visibility.
Correct code:
if ($('#box').is(':visible')) {
console.log('Visible');
}
2. Forgetting that .toggle() changes state immediately
Broken thinking:
$('#box').toggle();
if ($('#box').is(':hidden')) {
console.log('Box was hidden');
}
Why this is confusing:
Comparisons
Visibility methods and checks
| jQuery feature | Purpose | Changes state? | Returns a boolean? |
|---|---|---|---|
.hide() | Hide an element | Yes | No |
.show() | Show an element | Yes | No |
.toggle() | Switch visibility | Yes | No |
.is(':hidden') | Test whether hidden | No | Yes |
.is(':visible') | Test whether visible | No |
Cheat Sheet
Quick reference
$('#box').hide(); // hide
$('#box').show(); // show
$('#box').toggle(); // switch state
$('#box').is(':hidden'); // true if hidden
$('#box').is(':visible'); // true if visible
Common patterns
Show only if hidden
if ($('#box').is(':hidden')) {
$('#box').show();
}
Hide only if visible
if ($('#box').is(':visible')) {
$('#box').hide();
}
Toggle on click
$().(, () {
$().();
});
FAQ
How do I check if an element is hidden in jQuery?
Use:
$('#element').is(':hidden')
It returns true if the element is hidden.
How do I check if an element is visible in jQuery?
Use:
$('#element').is(':visible')
It returns true if the element is visible.
What is the difference between .toggle() and .is(':hidden')?
.toggle()changes visibility..is(':hidden')only checks visibility.
One acts, the other tests.
Should I use .css('display') === 'none' instead?
Usually, no. In jQuery code, :hidden and :visible are clearer and more reliable for checking visibility.
Does remove the element from the DOM?
Mini Project
Description
Build a small details panel interaction where a button shows and hides extra information. This demonstrates how to use .toggle(), .show(), .hide(), and .is(':visible') together in a realistic UI pattern.
Goal
Create a button that toggles a details section and updates its own text based on whether the section is visible.
Requirements
["Create a button and a details section in HTML.","Hide the details section when the page first loads.","When the button is clicked, toggle the details section.","Change the button text to match the current state: Show details or Hide details."]
Keep learning
Related questions
Deep Cloning Objects in JavaScript: Methods, Trade-offs, and Best Practices
Learn how to deep clone objects in JavaScript, compare structuredClone, JSON methods, and recursive approaches with examples.
Get Screen, Page, and Browser Window Size in JavaScript
Learn how to get screen size, viewport size, page size, and scroll position in JavaScript across major browsers with clear examples.
How JavaScript Closures Work: A Beginner-Friendly Guide
Learn how JavaScript closures work with simple explanations, examples, common mistakes, and practical use cases for real code.