One of the most popular uses for jQuery is creating simple visual effects. The
hide() and show() methods allow you to instantly control the
visibility of any element on your page with smooth, professional transitions.
The hide() method sets the CSS display property of an element
to none. This removes the element from the layout entirely.
$(selector).hide(speed, callback);
Example: Hiding a paragraph when a button is clicked.
$("#hideBtn").click(function() {
$("p").hide();
});
The show() method restores the element's visibility by resetting the
display property (usually to block or inline).
$(selector).show(speed, callback);
The toggle() method is a powerful shortcut. It checks the current state of
an element: if it is shown, it hides it; if it is hidden, it shows it. This saves you
from writing complex if...else logic.
$("#toggleBtn").click(function() {
$("p").toggle();
});
Both methods accept a **speed** parameter that controls how long the transition takes. You can use strings or milliseconds:
1000 for 1 second).// Hide with custom speed
$("#target").hide(1000); // Takes 1 second
$("#target").show("slow");
While you can hide elements with CSS display: none, jQuery's methods are
dynamic and include a "sliding and fading" effect that makes the transition look
smoother and more polished to the user.
toggle().The **callback** parameter is a function that runs *after* the hide or show effect is completely finished. This is useful for sequencing actions.
$("h1").hide("slow", function() {
alert("The heading is now gone!");
});
hide()/show() for logic
that needs to be triggered by user events. For static hidden content, stick to plain CSS to avoid "flicker" on page load.
display property.toggle() without checking the initial state. If an element starts hidden via CSS (display: none), the first toggle() click shows it — which can surprise you if you expected the opposite order.
display: none from the start instead.
hide() removes layout space. Unlike opacity: 0, hide() sets display: none, which collapses the element's space — surrounding content will shift to fill the gap.
A second example — using slideToggle()'s cousin, toggle(), to build a basic show/hide panel without any custom animation code:
<button id="panelBtn">Show Details</button>
<div id="panel" style="display:none; border:1px solid #ccc; padding:10px;">
Here are the extra details that were hidden by default.
</div>
<script>
$(function() {
$("#panelBtn").click(function() {
$("#panel").toggle("fast");
});
});
</script>
Q: Does hide() delete the element from the page?
A: No — the element stays in the DOM with display: none. You can bring it back instantly with show() without recreating it.
Q: What's the difference between hide() and CSS visibility: hidden?
A: hide() removes the element's layout space entirely; visibility: hidden keeps the space reserved but makes the content invisible.
Q: Can I animate a property other than visibility while hiding?
A: Yes — use slideUp()/slideDown() for a height-based animation instead. See the jQuery Effects API reference for the complete list.