Invisible Man wrote:
Thanks for help in advance...
I'm trying to change the background of:
<li class="noflyout" name="leftnav1"><a href="default.asp">Home
</a></li>
CSS set it to blue - but want to us JS to change it to red if
the user lands on the page... I've got the following JS:
document.getElementsByTagName("li").elements
["leftnav1"].style.backgroundColor= '#cc0000'
getElementsByTagName() returns a collection of all the elements in the
document with the tag name.
That is likely not the best approach here, it's better to replace NAME
attribute with an ID and use document.getElementById():
<li ... id="leftnav1">...</li>
...
document.getElementById("leftnav1").style.backgrou ndColor='#cc0000';
Read the group FAQ in regard to feature detection and incorporating
support for oder versions of IE with document.all.
<URL:http://www.jibbering.com/faq/#FAQ4_15>
Your statement should test that objects and methods are supported
before trying to use them and offer alternatives where appropriate:
if (document.getElementById ) {
var x = document.getElementById("leftnav1");
} else if (document.all ) {
var x = document.all["leftnav1"];
} else {
return
}
if ( x && x.style ) x.style.backgroundColor = '#cc0000';
If you intend using getElementById frequently, you may want to use
something like the DynWrite function mentioned at the above FAQ link.
--
Rob