Forum Moderators: open
I have an element that is initially hidden by default. When this element becomes visible, I want any second click to make this element become hidden once again. (I don't want the first click to make it hide, because the click that makes it visible would then hide it).
I've written the following script for the head of my document, but it doesn't seem the variable is remembered each time the script runs.
Variable "lame" is not defined elsewhere.
document.onclick=function(){
if(document.getElementById("s").style.visibility=="visible"){
if(lame=="1"){document.getElementById("s").style.visibility="hidden";lame="0";}
else{var lame="1";}
}}
Any help would be much appreciated.
Would this be more appropriate?
var lame="0";
document.onclick=function(){
if(document.getElementById("s").style.visibility=="visible"){
if(lame=="1"){document.getElementById("s").style.visibility="hidden";lame="0";}
else{var lame="1";}
}}
The problem is, it doesn't seem to remember that change either.
Am I moving in the right direction?
Thanks
I need to store the variable because I need the second click to turn off the visibility, otherwise the same click that turns it visible will also turn it invisible.
I have had a good deal of luck with this code:
var lame=0;
document.onclick=function(){
if(document.getElementById("s").style.visibility=="visible"){
if(lame==1){document.getElementById("s").style.visibility="hidden";lame--}
else{lame++}
}}
Your newer code no longer declares a variable at the local level. Therefore the references to lame in that function ARE affecting the globally declared variable. And that is what is needed.
[edited by: john_k at 3:30 pm (utc) on May 9, 2006]
I need to store the variable because I need the second click to turn off the visibility, otherwise the same click that turns it visible will also turn it invisible.
maybe i don't understand the problem fully, by why not simply do:
var el = document.getElementById('s');
if(el.style.visibility == 'visible')
el.style.visibility = 'hidden';
else if(el.style.visibility == 'hidden')
el.style.visibility = 'visible'; The whole code now looks like this:
var c=0;
function l(){var s=document.getElementById("s").style;if(s.visibility=="visible"){s.visibility="hidden";c--;}else{s.visibility="visible";}}
document.onclick=function(){var s=document.getElementById("s").style;if(s.visibility=="visible"){if(c==1){s.visibility="hidden";c--}else{c++}}}
(please note I changed "lame" to "c").
Now my concern is, I'm trying to optimize this script, but it doesn't seem to like it if I define "s" outside the functions, or if I add the visibility part to it. Any ideas? Or is this some kind of limitation I have to leave with?
This is what I would like, but it doesn't function:
var c=0;var s=document.getElementById("s").style.visibility;
function l(){if(s=="visible"){s="hidden";c--;}else{s="visible";}}
document.onclick=function(){if(s=="visible"){if(c==1){s="hidden";c--}else{c++}}}
it doesn't seem to like it if I define "s" outside the functions
i guess you mean "c"
Again, you need to remove the 'var' part, to make it global.
btw, could you indent your code a little when you post it. that makes it a lot more readable.
This is what I would like, but it doesn't function:
Same problem, you have to 'globally' define the variables by leaving out var
c=0;
s=document.getElementById("s").style.visibility;
function l()
{
if(s=="visible")
{
s="hidden";
c--;
}
else
{
s="visible";
}
}
document.onclick=function()
{
if(s=="visible")
{
if(c==1)
{
s="hidden";
c--;
}
else
{
c++;
}
}
}