1

I was wondering if anyone could figure out why my function won't work properly. What I am trying to achieve is when a button is clicked it displays text and when it is clicked again it hides it and so on.

function hideshow (){
    var showhide=document.getElementById('text');
    if(showhide.style.display="none")
    {
        showhide.style.display="block";
    }
    else{
        showhide.style.display="none";
    }
}

So far I got it to display my text when I click it once, but once I click it again it has no effect on the text.

BenR
  • 11,296
  • 3
  • 28
  • 47
user1512677
  • 33
  • 2
  • 4
  • 10
    `=` is an assignment operator... use `==` if you want to compare. Right now you're hiding it at the start of every click, then showing it again. Always. – TheZ Jul 10 '12 at 15:44
  • 2
    Or even better, try to use `===` to compare. Learn about it first, though: http://stackoverflow.com/questions/3735939/jslint-expected-and-instead-saw – jfrej Jul 10 '12 at 15:56

5 Answers5

2

I believe that should be:

function hideshow (){
    var showhide = document.getElementById('text');
    if (showhide.style.display == "none")
    {
        showhide.style.display = "block";
    }
    else{
        showhide.style.display = "none";
    }
}

So, use '==' instead of '=' when comparing. The '=' operator assigns a value. In javascript there is also the '===' operator. The difference is that '==' will cast values, while '===' will compare strictly.

For example:

0 == false; // will return true
0 === false; // will not

So you can also use

if (showhide.style.display === "none")

You can read more about the operators here.

Community
  • 1
  • 1
Peter
  • 13,733
  • 11
  • 75
  • 122
1

you should be using === in your if statment. = is an assignment operator:

function hideshow (){
    var showhide=document.getElementById('text');
    if(showhide.style.display==="none")
    {
        showhide.style.display="block";
    }
    else{
        showhide.style.display="none";
    }
}

Or:

function hideshow (){
    var showhide=document.getElementById('text');

    showhide.style.display = showhide.style.display === "none" ? 
        "block" : 
        "none";  
}
CD..
  • 72,281
  • 25
  • 154
  • 163
1

You should be using the comparison == operator instead of assigning the value using the = operator.

Try:

function hideshow() {
    var showhide = document.getElementById('text').style;
    (showhide.display = showhide.display == "none" ?  "block" : "none" )
}

You can assign and compare in one statement using:

(showhide.display = showhide.display == "none" ?  "block" : "none" )
                  ^assign            ^comparison

Demo: http://jsfiddle.net/7Eaf2/

Robert
  • 8,717
  • 2
  • 27
  • 34
0
function hideShow() {
    el.style.display != "none" ? "none" : "block";
}
Tom
  • 4,096
  • 2
  • 24
  • 38
  • Why does my text not display on my first click? I have my text set to onClick="hideshow();" but, it only runs on my second click. CSS for my text has display:none if that helps. – user1512677 Jul 12 '12 at 15:43
  • I still need to double click in order to get it to run. Don't worry about it, i'll search online for it. – user1512677 Jul 12 '12 at 16:00
0
<!DOCTYPE html>
<html>
<head>
    <title></title>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <script type="text/javascript">
        function hideshow() {
            var showhide=document.getElementById('text');
            if(showhide.style.visibility=="hidden")
            {
                showhide.style.visibility="visible";
            }
            else{
                showhide.style.visibility="hidden";
            }
        }
    </script>
</head>
<body>
    <div id="text">Text</div>
    <button onclick="hideshow()">hideshow</button>
</body>
</html>
Michael Besteck
  • 2,415
  • 18
  • 10