0

I found an example at https://www.w3schools.com/howto/tryit.asp?filename=tryhow_js_toggle_hide_show

<!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>

But it's Hide and Show. How should I modify it so I can do the reverse? First Show then Hide toggle?

Tomerikoo
  • 18,379
  • 16
  • 47
  • 61
modder
  • 9
  • 1
  • Does this answer your question? [How do you create a hidden div that doesn't create a line break or horizontal space?](https://stackoverflow.com/q/1992114/6045800) – Tomerikoo May 27 '21 at 14:33

2 Answers2

1

First, set displa:none; initially, so it's hidden by default.

Second, add new rule in if statement x.style.display === "", because on first click that is true.

Complete (working) code:

<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width, initial-scale=1">
<style>
#myDIV {
  display: none;
  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 === "") {
    x.style.display = "block";
  } else {
    x.style.display = "none";
  }
}
</script>

</body>
</html>

EDIT: Better solution

Use getComputedStyle(x).display instead of x.style.display, so there is no need to compare with empty string.

Updated code:

<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width, initial-scale=1">
<style>
#myDIV {
  display: none;
  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 (getComputedStyle(x).display === "none") {
    x.style.display = "block";
  } else {
    x.style.display = "none";
  }
}
</script>

</body>
</html>
Mirza Mašić
  • 119
  • 1
  • 6
0

I would suggest to add another class in your css

.is-hidden
{
    display:none;
}

and then simply toggle between is-hidden and nothing with

 document.getElementById("#myDIV").classList.toggle("is-hidden");

Simply add the is-hidden class at definition if you want to start with an hidden div

<div id="myDIV" class="is-hidden">
   This is my DIV element.
</div>
Carl Verret
  • 576
  • 8
  • 21