• 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 / JavaScript / Using JavaScript to Set Visibility

Using JavaScript to Set Visibility

April 30, 2022 Leave a Comment

We can use JavaScript to set the visibility of an element by using the JavaScript getElementById() method along with the visibility property.

document.getElementById("div1").style.visibility = "visible";

The code above will make sure #div1 is visible.

document.getElementById("div1").style.visibility = "hidden";

The code above will make sure #div1 is hidden from view, but it will still take up space.


Let’s say I have the following HTML:

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

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

document.getElementById("p1").style.visibility = "hidden";

The resulting HTML would be as follows:

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

Using JavaScript to Set the Visibility of a Div with a Click

We can set the visibility of an HTML element using JavaScript very easily by combining the visibility property 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 id="box1" class="boxes"></div>
  <div id="box2" class="boxes"></div>
  <div id="click-me" onclick="setVisibility()">Show/hide top box</div>
</div>

We can utilize both an onclick event and the JavaScript getElementById() 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 sets it to hidden. We will let the user change the visibility from visible to hidden and vice versa as many times as they want.

One thing to notice is when we set the visibility of an element to hidden, 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.

Here is our JavaScript code:

function setVisibility(){
  if ( document.getElementById("box1").style.visibility == "hidden" ){
    document.getElementById("box1").style.visibility = "visible";
  } else {
    document.getElementById("box1").style.visibility = "hidden";
  }
};

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

Code Output:

Show/hide top box

Full Code:


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

<script>

function setVisibility(){
  if ( document.getElementById("box1").style.visibility == "hidden" ){
    document.getElementById("box1").style.visibility = "visible";
  } else {
    document.getElementById("box1").style.visibility = "hidden";
  }
};

</script>

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

Other Articles You'll Also Like:

  • 1.  Grouping By Multiple Properties and Summing Using Javascript
  • 2.  Using JavaScript to Set the Id of an Element
  • 3.  Remove Brackets from String Using JavaScript
  • 4.  Using JavaScript to Check if String Contains Letters
  • 5.  Using JavaScript to Convert Float to Integer
  • 6.  Reverse a String in JavaScript
  • 7.  Using JavaScript to Change Text of Element
  • 8.  Examples Using the JavaScript += Addition Assignment Operator
  • 9.  Using JavaScript to get the Last Element in Array
  • 10.  clearInterval JavaScript – How the clearInterval() Method in JavaScript Works

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