I have this small menu. All I want is to open it with one click and then to close it with another click outside the html element.
I figured out only how to open it with a click but don’t know how to close it:
- HTML portion:
JavaScript
x
9
1
<div id="sign"onclick="setVisibility()">
2
login
3
<div id="user"></div>
4
<br>
5
<p id="userl">Streamer1228</p><br>
6
<div id="userll" >PREMIUM USER</p><br>
7
<button id="logout" >Logout</button>
8
</div>
9
- JavaScript code:
JavaScript
1
4
1
function setVisibility() {
2
document.getElementById("sign").style.height="350px";
3
}
4
Advertisement
Answer
One solution is using an if condition:
JavaScript
1
6
1
function setVisibility() {
2
if (document.getElementById("sign").style.height=="350px"){
3
document.getElementById("sign").style.height="0px";} else {
4
document.getElementById("sign").style.height="350px";}
5
}
6