-3

Script:

  let menu =  function(){
        var obj = document.getElementById("hidden")
        if(obj.style.display = "block"){
           obj.style.display = "none"
       //hide the div element with an ID #Hidden
        }else{
           obj.style.display = "block"
       //else show it 
        }
      }

        // I tried using buttons too, same result.

CSS

div#hidden{
       display:none;

    }

HTML:

<a href="javascript:menu()"id="info" > INFO </a> 

<div id="hidden">
     <!-- some stuff -->
</div>

I'm trying to "constantly" change the display property of my DIV element with an ID "hidden" to show and hide -VICEVERSA

my javascript code

Apparently this code runs but! it won't run or do anything to my code the second time this event is called.

the codes inside the if statements are executed but doesn't do anything or change my display property

Kakul Sarma
  • 303
  • 1
  • 4
  • 21
WEEFA
  • 1
  • 2

3 Answers3

1

Your if statement is incorrect. Do it as following

obj.style.display === "block"

here you can find more about comparison operator in javascript

PaulShovan
  • 2,140
  • 1
  • 13
  • 22
1

You should use the comparison operators (== or ===) in if statements, not the assignment operator (=)

Jesse
  • 3,522
  • 6
  • 25
  • 40
Mücahit Inel
  • 120
  • 11
0

see code below:

function func(){
var obj=document.getElementById("hidden");
if(obj.style.display==="block")
obj.style.display="none"
else
obj.style.display="block"
}
<p id="hidden" style="display:none">I am the text<p>
<button onclick="func()">click me</button>
לבני מלכה
  • 15,925
  • 2
  • 23
  • 47