Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

190
Views
html show hide div element

I'm trying to show/hide div in html now I'm using w3schools Toggle Hide and Show link. It is working good but by default it shows condition I what by default hide condition if I press show button it should show. help me.

code

<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width, initial-scale=1">
<style>
#myDIV {
  width: 100%;
  padding: 50px 0;
  text-align: center;
  background-color: lightblue;
  margin-top: 20px;
}
</style>
</head>
<body>

<p>Click the "Try it" button to toggle between hiding and showing the DIV element:</p>

<button onclick="myFunction()">Try it</button>

<div id="myDIV">
This is my DIV element.
</div>

<p><b>Note:</b> The element will not take up any space when the display property set to "none".</p>

<script>
function myFunction() {
  var x = document.getElementById("myDIV");
  if (x.style.display === "none") {
    x.style.display = "block";
  } else {
    x.style.display = "none";
  }
}
</script>

</body>
</html>
about 4 years ago · Juan Pablo Isaza
3 answers
Answer question

0

You need to make the following changes in your code. Add a display:none for #myDIV in css to hide it on initial loading. But if you still check in JS (x.style.display), you will get an empty string instead of none. That's beacause the style attribute does not have any setting for display until the code is run through the first time. So to handle this case you need to update your if condition as well like this - if (x.style.display == "none" || x.style.display === "")

You can check here for more info - Similar Question Link

I have attached a snippet. Hope that's how you want it to work.

function myFunction() {
  var x = document.getElementById("myDIV");
  if (x.style.display == "none" || x.style.display === "") {
    x.style.display = "block";
  } else {
    x.style.display = "none";
  }
}
#myDIV {
  width: 100%;
  display: none;
  padding: 50px 0;
  text-align: center;
  background-color: lightblue;
  margin-top: 20px;
}
<p>Click the "Try it" button to toggle between hiding and showing the DIV element:</p>

<button onclick="myFunction()">Try it</button>

<div id="myDIV">
This is my DIV element.
</div>

<p><b>Note:</b> The element will not take up any space when the display property set to "none".</p>

about 4 years ago · Juan Pablo Isaza Report

0

Add display: none to the div css,

#myDIV {
  width: 100%;
  padding: 50px 0;
  text-align: center;
  background-color: lightblue;
  margin-top: 20px;
  display: none;
}

and change the if condition to this:

if (x.style.display == "none" || x.style.display === "")
about 4 years ago · Juan Pablo Isaza Report

0

I suggest do not update the style property directly, but toggle a class "hidden" instead. Here is a snippet:

function myFunction() {
  const x = document.getElementById("myDIV");
  x.classList.toggle("hidden");
}
#myDIV {
  width: 100%;
  padding: 50px 0;
  text-align: center;
  background-color: lightblue;
  margin-top: 20px;
}

.hidden {
  display: none;
}
<!DOCTYPE html>
<html>
  <head>
    <meta name="viewport" content="width=device-width, initial-scale=1">
  </head>
  <body>

    <p>Click the "Try it" button to toggle between hiding and showing the DIV element:</p>

    <button onclick="myFunction()">Try it</button>

    <div id="myDIV" class="hidden">
      This is my DIV element.
    </div>
  </body>
</html>

If this does not solve your problem, please let me know ;)

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!