• 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 Show a Div

Using JavaScript to Show a Div

March 7, 2022 Leave a Comment

We can use JavaScript to show a div by combing the getElementById() method along with the Style display property.

document.getElementById("div1").style.display = "block";

Let’s say we have the following html:

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

Next, we want to show the div, so we will use the element’s display property. We do this simply by getting the id of the div and then changing its display property.

document.getElementById("div1").style.display = "block";

Note that we can also show a div easily using jQuery with the show() method.

Using JavaScript to Show a Div With a Click

We can use JavaScript to show a div very easily by combining the display property with an onclick event.

Let’s say that we have the following HTML where we want to give the user the ability to show a hidden div #div1. The div will just be a greenish box, that will have its display property set to “none” to start.

We will also provide a button to let the user be able to hide the div so they can show and hide the div as much as they want.

Here is the HTML code:


<style>#div1 { display: none; width: 300px; height: 200px; background: #7bbfa2; }</style><div>
  <div id="div1"></div>
  <div class="click-me" onclick="showDiv()">Show hidden div</div>
  <div class="click-me" onclick="hideDiv()">Hide div</div>
</div>

In the JavaScript code, we will add an onclick event to a button that will run a function we will create. In the function, we will simply change the display property of the div to “block”, which will show the div.

We will also have another function that will let the user hide the div.

Here is the JavaScript code:

function showDiv(){
  document.getElementById("div1").style.display = "block";
}; 
function hideDiv(){
  document.getElementById("div1").style.display = "none";
}; 

The final code and output for this example of using JavaScript to show a div with a click is below:

Code Output:

Show hidden div
Hide div

Full Code:

<style>#div1 { display: none; width: 300px; height: 200px; background: #7bbfa2; }</style><div>
  <div id="div1"></div>
  <div class="click-me" onclick="showDiv()">Show hidden div</div>
  <div class="click-me" onclick="hideDiv()">Hide div</div>
</div>

<script>

function showDiv(){
  document.getElementById("div1").style.display = "block";
}; 
function hideDiv(){
  document.getElementById("div1").style.display = "none";
}; 

</script>

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

Other Articles You'll Also Like:

  • 1.  innerHTML vs outerHTML
  • 2.  Using JavaScript to Get URL Hash
  • 3.  How to Use JavaScript to Change Button Text
  • 4.  Using JavaScript to Check if Array is Empty
  • 5.  Using JavaScript to Check If String Contains Only Certain Characters
  • 6.  Using JavaScript to Get the Domain From URL
  • 7.  How to Uncheck All Checkboxes in JavaScript
  • 8.  Using JavaScript to Check if Number is Divisible by Another Number
  • 9.  Convert an Array to Set in JavaScript
  • 10.  Push Multiple Items to Array in JavaScript

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