PDA

View Full Version : Problem with simple DOM show/hide script


Luke Redpath
06-17-2003, 01:39 PM
My javascript knowledge is rather limited and I'm struggling with a simple show/hide script. The following works:


function Toggle(idtotoggle) {
if (document.getElementById)
{
var element = document.getElementById(idtotoggle)
element.style.display = "block";
}
}


This changes the style display attribute of an element from hidden (default inline style) to block. This is fine.

However, I want to add a simple if...else statement to this so that if the display attribute is hidden, it changes it to block, and vice versa. I've done this:


function Toggle(idtotoggle) {
if (document.getElementById)
{
var element = document.getElementById(idtotoggle)
if (element.style.display = "hidden");
{
element.style.display = "block";
} else
{
element.style.display = "hidden";
}
}
}


However this does not work. I get the javascript error:

"Could not get the display property. Invalid argument"

I'm baffled but I'm sure it's something rather simple.

Any help is appreciated, thanks.

Skyzyx
06-17-2003, 04:55 PM
You've posted this in the wrong forum. You didn't read the directions. tsk, tsk.

brothercake
06-17-2003, 05:03 PM
if (element.style.display = "hidden");

What that does is assign the value "hidden" to the display property; what you mean is

if (element.style.display == "hidden");

Vladdy
06-17-2003, 05:16 PM
display - none
visiblity - hidden

Luke Redpath
06-17-2003, 08:07 PM
Thanks, and sorry for posting this in the wrong forum. Missed the warning and misread the name of the forum- trying to browse behind boss's back at work! :)