• 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 / Toggle the Visibility of an Element Using jQuery

Toggle the Visibility of an Element Using jQuery

March 16, 2022 Leave a Comment

We can use jQuery to toggle the visibility of an element by using the jQuery css() method and an if-else statement.

if ( $("#div1").css("visibility") == "hidden" ){
  $("#div1").css("visibility","visible");
} else {
  $("#div1").css("visibility","hidden");
}

Let’s say I have the following HTML:

<div id="div1">
  <p class="p">This is a paragraph.</p>
</div>

If we wanted to use jQuery to toggle the visibility of the paragraph inside of div #div1, we would use the following JavaScript code:

if ( $(".p").css("visibility") == "hidden" ){
  $(".p").css("visibility","visible");
} else {
  $(".p").css("visibility","hidden");
}

The resulting HTML would be as follows:

<div id="div1">
  <p class="p" style="visibility: hidden;">This is a paragraph.</p>
</div>

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

if ( jQuery(".p").css("visibility") == "hidden" ){
  jQuery(".p").css("visibility","visible");
} else {
  jQuery(".p").css("visibility","hidden");
}

Using jQuery to Toggle the Visibility of a Div with a Click

We can toggle the visibility of an HTML element using jQuery very easily by combining the css() method with a click event.

In this HTML example, we will have two boxes stacked on top of each other. We will provide a button to give the user the option to change the visibility of the top box.

Here is the simple HTML code:


<style>.boxes { width: 200px; height: 200px; margin-bottom: 10px; } .box1 { background: #82b5be; } .box2 { background: #9c94d5; } </style>
<div id="div1">
  <div class="box1 boxes"></div>
  <div class="box2 boxes"></div>
  <div id="click-me">Toggle top box visibility</div>
</div>

We can utilize both the jQuery click() method and jQuery css() method to toggle the visibility of the top box. When the user clicks the button, we will run a function that changes the visibility property of the div and changes it to hidden. We will toggle this by using the code we have in the above example.

One thing to notice is when we toggle the visibility of an element it will hide the element from view, but the element will still take up space on the screen. If we want to hide the element and remove it from taking up space on the screen, we would have to target the display property of the element and change it to none.

$("#click-me").click(function(){
  if ( $(".box1").css("visibility") == "hidden" ){
    $(".box1").css("visibility","visible");
  } else {
    $(".box1").css("visibility","hidden");
  }
});

The final code and output for this example of how to use jQuery to toggle the visibility of a div with a click is below:

Code Output:

Toggle top box visibility

Full Code:


<style>.boxes { width: 200px; height: 200px; margin-bottom: 10px; } .box1 { background: #82b5be; } .box2 { background: #9c94d5; } </style>
<div id="div1">
  <div class="box1 boxes"></div>
  <div class="box2 boxes"></div>
  <div id="click-me">Toggle top box visibility</div>
</div>

<script>

$("#click-me").click(function(){
  if ( $(".box1").css("visibility") == "hidden" ){
    $(".box1").css("visibility","visible");
  } else {
    $(".box1").css("visibility","hidden");
  }
});

</script>

Hopefully this article has been useful for you to understand how to use jQuery to toggle the visibility of an element.

Other Articles You'll Also Like:

  • 1.  Using jQuery to Count Children of an Element
  • 2.  jQuery focus – Changing Form Styles with the focus() Method
  • 3.  jQuery siblings – Get the Sibling Elements of a Div
  • 4.  jQuery val Method – Get the Value from an Input Field
  • 5.  Get the Height of a Div Using jQuery
  • 6.  jQuery contains – How to Check if a Paragraph Contains Specific Text
  • 7.  Using jQuery to Capitalize the First Letter of a String
  • 8.  Using jQuery to Get Element by ID
  • 9.  Using jQuery to Add Option to Select List
  • 10.  Using the jQuery fadeTo Method to Change the Opacity 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

x