• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar

The Programming Expert

Solving All of Your Programming Headaches

  • HTML
  • JavaScript
  • jQuery
  • PHP
  • Python
  • SAS
  • Ruby
  • About
You are here: Home / jQuery / Using jQuery to Show a Div

Using jQuery to Show a Div

November 24, 2021 Leave a Comment

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:

<div id="div1">This is a hidden div that I can show with jQuery</div>

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:

<style>#div-2 { display:none; }</style><div>
  <div id="div-1">Div 1</div>
  <div id="div-2">Div 2</div>
  <div id="click-me">Show Div 2</div>
</div>

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:

<style>#div-2 { display:none; }</style>
<div>
  <div id="div-1">Div 1</div>
  <div id="div-2">Div 2</div>
  <div id="click-me">Show Div 2</div>
</div>

<script>

$("#click-me").click(function(){
  $("#div-2").show();
});

</script>

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:

<style>#div-2-1 { display:none; }</style>
<div>
  <div id="div-1">Div 1</div>
  <div id="div-2-1">Div 2</div>
  <div id="click-me-1" class="click-me">Show Div 2</div>
</div>

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:

<style>#div-2-1 { display:none; }</style>
<div>
  <div id="div-1">Div 1</div>
  <div id="div-2-1">Div 2</div>
  <div id="click-me-1" class="click-me">Show Div 2</div>
</div>

<script>

$("#click-me-1").click(function(){
  $("#div-2-1").css("display","block");
});

</script>

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

Other Articles You'll Also Like:

  • 1.  jQuery clone – Making a Copy of an Element
  • 2.  How to Use jQuery to Change Button Text
  • 3.  Using jQuery to Change Text of HTML Element
  • 4.  Using jQuery to Add Required Attribute to Input Field
  • 5.  Add Style to HTML Element Using jQuery
  • 6.  Using jQuery to Set Select to the First Option
  • 7.  Using jQuery to Change the Id of a Div
  • 8.  Using jQuery to Get the Bottom Position of Element
  • 9.  Using jQuery to Change Background Color
  • 10.  Using jQuery to Change Label Text

About The Programming Expert

The Programming Expert is a compilation of a programmer’s findings in the world of software development, website creation, and automation of processes.

Programming allows us to create amazing applications which make our work more efficient, repeatable and accurate.

At the end of the day, we want to be able to just push a button and let the code do it’s magic.

You can read more about us on our about page.

Reader Interactions

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Primary Sidebar

About The Programming Expert

the programming expert main image

Welcome to The Programming Expert. We are a group of US-based programming professionals who have helped companies build, maintain, and improve everything from simple websites to large-scale projects.

We built The Programming Expert to help you solve your programming problems with useful coding methods and functions in various programming languages.

Search

Learn Coding from Experts on Udemy

Looking to boost your skills and learn how to become a programming expert?

Check out the links below to view Udemy courses for learning to program in the following languages:

Copyright © 2023 · The Programming Expert · About · Privacy Policy