Visibility Effects

Explore the visibility effects in jQuery such as hide(), show(), and toggle() along with relevant examples.

Visibility effects dynamically hide or show certain elements on a webpage. The relevant effects are discussed in detail below.

hide() effect

Given a selector, the hide() effect temporarily hides the set of matched elements from a webpage.

In the “Hide Div” button event handler below, we hide all the div elements using the hide() effect in line 3.

show() effect

Given a selector, the show() effect displays the hidden elements present in the set of matched elements back on the webpage.

In the example “Show Div” button event handler below, we display all the hidden div elements using the show() effect in line 7.

toggle() effect

The toggle() effect is a combination of hide() and show() effects. Given a selector, if the matched elements are in a visible state, the toggle() effect hides them from the webpage. Similarly, if the matched elements are in a hidden state, the effect displays them back on the webpage.

Observe this behavior in the example below. In the “Toggle Div” button event handler, we toggle between hiding and revealing div elements using the toggle() effect in line 3.

Optional speed parameter

All jQuery effects take an optional speed parameter that controls the duration of the effect. The speed parameter can be set to “fast,” “slow,” or an exact number of milliseconds that specify the effect’s duration.

In the below example, we have multiple buttons with multiple event handlers. Each button has differently specified durations of its hide() and show() effects. Play around with the web page to visualize the difference in speed.