April 14, 2012

on click show div getting error

Question by user1160126

am very sorry for this new bie question, when i click on the lick the div should open when i click again it should close the div..please help me on this

<html>
    <head>
        <script type="text/javascript">
        var bool = 0;
            function showDiv(){
                if(bool==1){
                    bool=0;
                    document.getElementById(show).style.visibility = "hidden";
                }else if(bool==0){
                    bool=1;
                    document.getElementById(show).style.visibility = "visible";
                }
            }
        </script>
    </head>
    <body>
     <input type="button" value="click" onclick="showDiv();" />

        <div="show">
            <p>it is okay it is okay it is okay it is okay it is okay it is okay it is okay it is okay</p>
        </div>

    </body>
</html>

Answer by Gabe

You’re missing the quotes for the id argument for getElementById()

document.getElementById('show').style.visibility = "hidden";

Also the id attribute name is missing on the <div>

 <div="show">

Should be this:

<div id="show">

jsFiddle

Answer by Starx

When you are referring to

document.getElementById(show).style.visibility

The show refers to a variable, but you are trying to get it as a string, so you should get it quoted

document.getElementById('show').style.visibility

Author: Nabin Nepal (Starx)

Hello, I am Nabin Nepal and you can call me Starx. This is my blog where write about my life and my involvements. I am a Software Developer, A Cyclist and a Realist. I hope you will find my blog interesting. Follow me on Google+

...

Please fill the form - I will response as fast as I can!