+ 3
How to make a div appear and disapper with 2 clicks on js
Guys, please I need help with using a click to make a div appear and disappear by tapping a button twice. Like first time, it appears, second it disappears. Have done the appearing, now the second click
12 Réponses
+ 2
By the way, wouldn't it be easier to just switch based on its state?
con.style.display = con.style.display == "none"? "block": "none";
+ 3
You can make any DOM element disappear by setting its visibility property to "hidden" (default: "visible"). Then you can make a function that switches between these two states
+ 3
Thank you but I know that already
+ 3
see this
function cont(){
var count=0;
count++;
if (count=1){
var con=document.getElementById("con");
con.style.display="block";
}
else if (count=2){
var con=document.getElementById("");
con.style.display="none";
count=0;
}
}
+ 3
the problem is that everytime the function runs, you automatically reset count's value; you should define count outside of the function. Also, you didn't get the id of anything in the second function
+ 3
Have done that, still not working
+ 3
var count=0;
function cont(){
count++;
var con=document.getElementById("con");
if (count=1){
con.style.display="block";
}
else{
con.style.display="none";
count=0;
}
}
+ 3
Please explain
+ 3
ooh,let me try it
+ 3
Ooh, thanks a lot Airree. I've gotten it. The final code, from your help:
function cont(){
var con=document.getElementById("con");
if (con.style.display=="none"){
con.style.display="block";
}
else{
con.style.display="none";
}
}
+ 2
It checks if the display of con is none, if it is, it sets it to block, otherwise sets it to none
+ 1
Can you, like let me see your code?