PDA

View Full Version : Is this possible?


arthell
06-06-2005, 10:29 AM
I've only just started reading up on Javascript/DHTML/etc. so there's a good chance this is just a simple mistake on my part, but i can't for the life of me get this to work. After two days of searching I haven't found much help.

What I'm trying to do is create a single javascript function that I can use to toggle one style (visibility) on multiple divs, but at different times.

The basic idea behind the function:


function switch()
{
if (document.getElementById('divrecievedfromvariable').style.visibility == 'hidden')
{ document.getElementById('divrecievedfromvariable').style.visibility = 'visible' }
else
{ document.getElementById('divrecievedfromvariable').style.visibility = 'hidden' }
}


What I originally tried to do with the above was to update a variable during the onClick, which I then tried to insert into the getElementById(), but I've had zero luck with every method I've tried.

Please tell me there's a way to stick a variable inside there. :D

Thanks in advance for any help.

Kor
06-06-2005, 02:50 PM
If you try to use style.attribute's value in a condition, you must be sure that the object has a style locally defined. If your CSS codes are embeded or external, you may use a trick, an extra condition which will check if that style attribute exists or not.


function switch(id) {
var obj = document.getElementById(id);
obj.style.visibility=((obj.style.visibility=='visible')||(!obj.style.visibility)) ? 'hidden' : 'visible';
}

mrruben5
06-17-2005, 03:30 AM
You could also use className for it instead of style, and set inside the css, then you won't have to do this for each div you want to click and such.