Forum Moderators: open
function toggleDisplay(id) {
var el = getElementById(id);
el.style.display = (el.style.display=="block")? "none" : "block";
}
The script works but only on the second call. When I add alert(el.style.display) to see whats happening it's showing that the style is blank on the first call which the function then sets to "block" and on the second call it starts working correctly.
I've explicitly added display:block in the CSS style sheet for the elements I'm using this on, thinking that was why it was returning blank, but that didn't solve anything. It works just fine when I add the styles inline (i.e. style="display:block;").
I'm testing this in Firefox 2.0.0.9 mac and Safari 3.
Have a read of this thread:
[webmasterworld.com...]
IMO, in your case, I would simply decide on your default value and have this as your 'else' value.
var el = getElementById(id);
Does this work on the Mac? This gives an error on Windows FF2 and IE6. getElementById() is a method of the document object, so should be called as:
var el = document.getElementById(id);
el.style.display = (el.style.display=="block")? "none" : "block";
The problem is really just syntax, instead of trying to compare what the display property is, try working out what it isn't, and use the 'alternate' value in your expression:
el.style.display = (el.style.display!= "none"? "none" : "block");
This will still work if the value is empty, and prevent you having to specify a default.
I moved the brackets really just personal preference.