The three methods
.hide()— hide elements.show()— show hidden elements.toggle()— hide if visible, show if hidden
Speed
Add a speed to animate: 'slow', 'fast' or milliseconds:
$('#info').hide(600);
Callback
A function that runs after the effect finishes:
$('#info').hide('slow', function () {
alert('Hidden!');
});
Check visibility
$('#info').is(':visible') returns true or false.
These methods change display, so hidden elements take no space.
Example
Example — Read more / read less
HTML
<script src="https://code.jquery.com/jquery-3.7.1.min.js"></script>
<p>jQuery makes simple effects very easy.
<span id="more" style="display:none">With one line you can hide, show or toggle any element, add speed and even run code after the effect ends.</span>
</p>
<button id="toggle">Read more</button>
<script>
$(function () {
$('#toggle').click(function () {
const $btn = $(this);
$('#more').toggle('fast', function () {
$btn.text($('#more').is(':visible') ? 'Read less' : 'Read more');
});
});
});
</script>