lundi 5 février 2018

Javascript if statement with boolean

Im trying to create a button that make my div element show or hide, for open and close it with the same button. I dont get why it does not works, when I click on the button nothing happens, can someone help me please?

<!DOCTYPE html>
<html>
<head>
<style> 
#myDIV {
    width: 500px;
    height: 500px;
    background-color: lightblue;
    display: none;
}
</style>
</head>
<body>

<p>Click the "Try it" button to set the display property of 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>

var div_aperto = false;

function myFunction(div_aperto) 

   if (div_aperto) { 
       div_aperto = false;
       document.getElementById("myDIV").style.display = "none"; }   
   
   else { div_aperto = true;
       document.getElementById("myDIV").style.display = "block"; } 
      
</script>

</body>
</html>

Aucun commentaire:

Enregistrer un commentaire