• 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 and Hide a Div

Using jQuery to Show and Hide a Div

March 9, 2022 Leave a Comment

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.

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

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' ){
  $("#div1").show()
} else {
  $("#div1").hide();
}

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:

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

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

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

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

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

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:


<style>#div1 { width: 300px; height: 200px; background: #7bbfa2; }</style>
<div>
  <div id="div1"></div>
  <div id="click-me">Show/hide</div>
</div>

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:

$("#click-me").click(function(){
  $("#div1").toggle();
}); 

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

Code Output:

Show/hide

Full Code:

<style>#div1 { width: 300px; height: 200px; background: #7bbfa2; }</style>
<div>
  <div id="div1"></div>
  <div id="click-me">Show/hide</div>
</div>

<script>

$("#click-me").click(function(){
  $("#div1").toggle();
});

</script>

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

Other Articles You'll Also Like:

  • 1.  Using jQuery to Replace a Class
  • 2.  How to Use jQuery to Remove Element
  • 3.  jQuery keydown Method
  • 4.  Using the jQuery fadeTo Method to Change the Opacity of an Element
  • 5.  Using jQuery prepend to Insert Element As First Child
  • 6.  jQuery slideUp – How to Slide Up and Hide an Element Using jQuery
  • 7.  Using jQuery to Wait 1 Second Before Executing Code
  • 8.  Using jQuery to Change the Text Color of a Paragraph
  • 9.  jQuery fadeOut – Fading Out Element in HTML
  • 10.  Using jQuery to Get the Margin of an Element

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