How to toggle hide and show

Use JavaScript to toggle between hide and show elements.

Click the button!

Toggle (hide/show) element

First step - Add HTML:

<button onclick="myFunction()">Click Me</button>
<div id="myDIV">
  This is my DIV element.
</div>

Second step - Add JavaScript:

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

Prova personalmente

Pagine correlate

Tutorial:Visualizzazione CSS