Forum Moderators: open
I'm trying to duplicate the menu setup at Webmonkey.com, but I want to be able to use some javascript to tell the menu to highlight the current page. I noticed you can do something like:
function color_this(id){
document.all[id].style.color="#ffcc33";
}
Which will change the text color of the div id you pass it.
But you can't do something like:
function color_this(id){
document.all[id].style.background-color="#ffcc33";
}
I'm guessing that javascript is reading the "-" as a minus sign. Anyone know a better way to do this? I can post the code for the entire page if need be.
document.all[id].style.backgroundColor="#ff33cc"
or, even just
document.all[id].style.background="#ff33cc"
When changing styles using JavaScript you do not add a hyphen. Instead, each part of the word gets capitalized.
z-index would be zIndex
text-decoration would be textDecoration
font-family would be fontFamily
etc.
In your example you're just using document.all, which is IE's implementation in older browsers. It only works in Opera as long as the User-Agent says that it's IE. What you should do is first check whether document.getElementById is supported. If not, you can use document.all.
if(document.getElementById) {
document.getElementById('idname').style.backgroundColor="#ff33cc";
}
else if(document.all) {
document.all['idname'].style.backgroundColor="#ff33cc";
}