user1160126 user1160126 - 4 months ago 7
Javascript Question

on click show div getting error

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

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