There are a couple of ways we can use jQuery to show and hide a div. The simplest way is to use the jQuery toggle() method.


We can also use the jQuery show() and hide() methods to show/hide a div along with an if else conditional statement.

if ( $("#div1").css("display") == 'none' ){
} else {

We will use the jQuery toggle() method over other ways to show/hide a div as we find it the easiest one to use.

Let’s say we have the following html:

This is a div that we can show/hide with jQuery

If we want to use jQuery to show/hide the div, we can do so by using the jQuery toggle() method.


If you are using WordPress, don’t forget to change the $ to jQuery as below:


Note that we can also show/hide (or toggle) a div easily using plain JavaScript by targeting an element’s display property.

Using jQuery to Show/Hide a Div With a Click

We can use jQuery to show and hide a div very easily by combining the toggle() method with the click() method.

Let’s say that we have the following HTML where we want to give the user the ability to show and hide the div #div1. The div will just be a greenish box that will be shown to start.

Here is the HTML code:


In the JavaScript code, when a user clicks the button, we will run a function that we will create. In the function, we will simply apply the toggle method to the div, which will hide it if it is shown, or show the div if it is hidden.

Here is the JavaScript code:


The final code and output for this example of how to show/hide a div using jQuery is below:

Code Output:


Full Code:


Hopefully this article has been useful for you to understand how to use jQuery to show/hide a div.

Categorized in:


Last Update: February 26, 2024