0

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>

6 Answers6

2

You don't have the brackets for function statement and you don't need argument

function myFunction() {

   if (div_aperto) { 
       div_aperto = false;
       document.getElementById("myDIV").style.display = "none"; }   

   else { div_aperto = true;
       document.getElementById("myDIV").style.display = "block"; } 
}
dshukertjr
  • 15,244
  • 11
  • 57
  • 94
2

Beyond the missing {}, note your function accepts an argument, but your sending it nothing (myfunction()). This will cause the div_aperto to always be undefined when the function starts. If you want to reference the global drop the argument.

<!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()  {
   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>
kabanus
  • 24,623
  • 6
  • 41
  • 74
1

It seems you missed the opening and closing brackets of the function definition.

I also linked a working fiddle, try it out. :)

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

https://jsfiddle.net/xmzh92z9/1/

Belian
  • 114
  • 8
0

The problem is you're shadowing div_aperto in the declaration of your function. Its value is thus always undefined in the function.

Change

function myFunction(div_aperto) 

to

function myFunction() {

You also need to add the braces around the function body.

Demonstration:

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

var div_aperto = false;

function myFunction() {

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



</body>
</html>
Denys Séguret
  • 372,613
  • 87
  • 782
  • 758
0

You need to put all the sentences of the myFunction between {}, also if you declare your div_aperto variable as global, you don't have to pass it as argument to the function:

<!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() {
   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>
Javier Gonzalez
  • 915
  • 6
  • 14
-1

Simple. Remove your script code and add bellow script

<script>

var div_aperto = false;

function myFunction() 
{
   if (div_aperto) { 
       div_aperto = false;
       document.getElementById("myDIV").style.display = "none"; }   

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