How do I access the first ul's style via DOM2 from the following code?:
<body>
<div id="foo">
<ul>
<li><a href="#>foobar</a></li>
</ul>
<ul>
<li><a href="#>foobar</a></li>
</ul>
</div>
</body>
There are a couple of ways: using getElementsByTagName or walking the
document tree. Either way will start with:
if(document.getElementById) {
var foo = document.getElementById('foo');
/* Insert here... */
}
and use:
if(foo.getElementsByTagName) {
var lists = foo.getElementsByTagName('UL');
if(lists.length) {
var item = lists[0], style;
if((style = item.style)) {
style.<...> = ...;
}
}
}
or:
var item = foo.firstChild, style;
while(item && (1 != item.nodeType)) {item = item.nextSibling;}
if(item && (style = item.style)) {
style.<...> = ...;
}
Of course, this assumes that your document structure will be exactly as
indicated. I'm also being more defensive than I possibly need to be.
Hope that helps,
Mike
Code only reviewed, not tested.