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

$("#div1").show();

We can also use the jQuery css() method to show a div.

$("#div1").css("display", "block");

Let’s say we have the following html:

This is a hidden div that I can show with jQuery

Next, we want to show the div, so we will use the jQuery show() method. If we wanted to show the div when the web page initially loads the JavaScript file, it would look like:

$(document).ready(function() {
  $("#div1").show();
});

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

jQuery("#div1").show();

Another way you can show a div using jQuery is to use the jQuery css() method:

$("#div1").css("display", "block");

The jQuery css() method is very useful for changing the styling of a html element dynamically – for example, changing the background color of an html element.

Note that we can also show a div in just plain JavaScript using the display property.

Using jQuery to Show a Div With a Click

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

Let’s say that we have the following html where we want to give the user the ability to show the hidden div #div-2:

Div 1
Div 2
Show Div 2

We can utilize both the jQuery click() method and jQuery show() method to show #div-2.

Here is the JavaScript code:

$("#click-me").click(function(){
  $("#div-2").show(); // Results in the element #div-2 being shown
}); 

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

Code Output:

Div 1
Div 2

Show Div 2

Full Code:


Div 1
Div 2
Show Div 2

Using the jQuery css() Method to Show a Div

The jQuery css() method is incredibly useful when using JavaScript to manipulate web pages.

We can use the css() method to show a hidden div.

Let’s say that we have the following html (the same the above example) where we want to give the user the ability to show the hidden #div-2-1:


Div 1
Div 2
Show Div 2

We will utilize both the jQuery click() method again with the jQuery css() method to show #div-2-1.

Below is the JavaScript code which will allow the user to be able to show the hidden div.

$("#click-me").click(function(){
  $("#div-2-1").css("display","block"); // Results in the element #div-2-1 being shown
}); 

The final code and output for this example is below:

Code Output:

Div 1
Div 2

Show Div 2

Full Code:


Div 1
Div 2
Show Div 2

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

Categorized in:

jQuery,

Last Update: February 26, 2024