In this chapter, we'll explore the powerful features that jQuery provides to hide and show elements on a webpage. We'll cover the methods .hide(), .show(), and .toggle(), starting from the basics and moving towards more advanced usage. By the end of this chapter, you'll have a comprehensive understanding of these methods, and you'll be able to manipulate the visibility of HTML elements dynamically using jQuery.
The .hide()
method is used to hide the selected elements. This method changes the display
property of the element to none
, effectively making it invisible on the webpage.
$(selector).hide();
jQuery .hide() Example
This is a paragraph of text that will be hidden when you click the button.
text
and a button with the ID hideButton
..hide()
method.The .hide()
method can also take two optional parameters: duration
and complete
.
$(selector).hide(duration, complete);
duration
(optional): A string or number determining how long the animation will run. Default is 400 milliseconds.complete
(optional): A callback function to call once the animation is complete.
jQuery .hide() with Duration and Callback
This is a paragraph of text that will be hidden slowly when you click the button.
The .show()
method is used to display the selected elements that are hidden. This method changes the display
property of the element to its default or specified value, making it visible.
$(selector).show();
jQuery .show() Example
This is a paragraph of text that will be shown when you click the button.
display: none;
)..show()
method.The .show()
method can also take two optional parameters: duration
and complete
.
$(selector).show(duration, complete);
duration
(optional): A string or number determining how long the animation will run. Default is 400 milliseconds.complete
(optional): A callback function to call once the animation is complete.
jQuery .show() with Duration and Callback
This is a paragraph of text that will be shown slowly when you click the button.
The .toggle()
method is used to toggle the visibility of the selected elements. If the elements are visible, .toggle()
will hide them, and if they are hidden, .toggle()
will show them.
$(selector).toggle();
jQuery .toggle() Example
This is a paragraph of text that will be toggled when you click the button.
The .toggle()
method can also take two optional parameters: duration
and complete
.
$(selector).toggle(duration, complete);
duration
(optional): A string or number determining how long the animation will run. Default is 400 milliseconds.complete
(optional): A callback function to call once the animation is complete.
jQuery .toggle() with Duration and Callback
This is a paragraph of text that will be toggled slowly when you click the button.
.hide()
method hides the selected elements..show()
method shows the selected elements..toggle()
method toggles the visibility of the selected elements.duration
and complete
for adding animations and callback functions.In this section, we’ll look at some practical examples that demonstrate how to use .hide()
, .show()
, and .toggle()
methods in real-world scenarios. These examples will help solidify your understanding and show you how to apply these methods in various contexts.
A common use case for .toggle()
is in creating an FAQ (Frequently Asked Questions) section where answers are hidden until a question is clicked.
FAQ Section Example
What is jQuery?
jQuery is a fast, small, and feature-rich JavaScript library.
How do you include jQuery in a project?
You can include jQuery by linking to the jQuery CDN or downloading the library.
What are the benefits of using jQuery?
jQuery simplifies HTML document traversal, event handling, and animation.
.question
) and answers (.answer
)..answer
elements are hidden by default using CSS..question
element is clicked, the corresponding .answer
element is toggled with a 500 milliseconds animation.In many web applications, a login form is hidden by default and displayed only when the user chooses to log in.
Login Form Example
Login Form
#loginForm
) is hidden by default using CSS.In an image gallery, you might want to show one image at a time and hide the rest.
Image Gallery Example
.active
class).Collapsible content sections are useful for organizing large amounts of information, such as in a blog post or documentation.
Collapsible Sections Example
Section 1
This is the content of section 1.
Section 2
This is the content of section 2.
Section 3
This is the content of section 3.
.header
) and content (.content
).In this chapter, we have explored the .hide(), .show(), and .toggle() methods in jQuery. These methods provide a simple yet powerful way to control the visibility of elements on a webpage, enhancing the interactivity and dynamism of web applications.Happy coding !❤️