• 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 / How to Hide a Div in JavaScript

How to Hide a Div in JavaScript

March 8, 2022 Leave a Comment

We can hide a div in JavaScript easily by combing the getElementById() method along with the Style display property.

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

Let’s say we have the following html:

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

Next, we want to hide 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 to “none”.

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

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

Hiding a Div in JavaScript With a Click

We can use JavaScript to hide 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 hide a div #div1. The div will just be a greenish box with set dimensions.

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

Here is the HTML code:


<style>#div1 { width: 300px; height: 200px; background: #7bbfa2; }</style><div>
  <div id="div1"></div>
  <div id="click-me" onclick="hideDiv()">Hide div above</div>
  <div id="click-me" onclick="showDiv()">Show hidden 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 “none”, which will hide the div.

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

Here is the JavaScript code:

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

The final code and output for this example of hiding a div in JavaScript with a click is below:

Code Output:

Hide div above
Show hidden div

Full Code:

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

<script>

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

</script>

Hopefully this article has been useful for you to understand how to hide a div in JavaScript.

Other Articles You'll Also Like:

  • 1.  JavaScript offsetTop – Get the Top Position of Element
  • 2.  Using JavaScript to Get Substring Between Two Characters
  • 3.  Using JavaScript to Remove Substring From String
  • 4.  Using JavaScript to Create a Unique Array
  • 5.  How to Call a JavaScript Function From HTML
  • 6.  Remove the Last Element From Array in JavaScript
  • 7.  Using JavaScript to Get a Random Number Between 1 and 100
  • 8.  lastIndexOf JavaScript – Get the Last Position of a String in Another String
  • 9.  Creating a JavaScript Function to Add Two Numbers
  • 10.  Using JavaScript to Check if Variable is a String

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