Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > HTML > How to access ul element's style via DOM2?

Reply
Thread Tools

How to access ul element's style via DOM2?

 
 
Spartanicus
Guest
Posts: n/a
 
      11-09-2004
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>

document.???.[0].style.display="block"

--
Spartanicus
 
Reply With Quote
 
 
 
 
Michael Winter
Guest
Posts: n/a
 
      11-09-2004
On Tue, 09 Nov 2004 12:00:54 +0000, Spartanicus <> wrote:

> 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.

--
Michael Winter
Replace ".invalid" with ".uk" to reply by e-mail.
 
Reply With Quote
 
 
 
 
DU
Guest
Posts: n/a
 
      11-10-2004
Spartanicus wrote:

> 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>
>
> document.???.[0].style.display="block"
>


The easiest way would be

<ul>
<li id="idFirstLi"><a href="#">foobar</a></li>
</ul>

document.getElementById("idFirstLi").style.display = "block";

By the way, you're missing closing quotes on the href attribute values.

DU
--
The site said to use Internet Explorer 5 or better... so I switched to
Mozilla 1.7.3
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
access Access via PIX ? barret bonden Cisco 1 07-28-2008 06:18 AM
All style tags after the first 30 style tags on an HTML page are not applied in Internet Explorer Rob Nicholson ASP .Net 3 05-28-2005 03:11 PM
How to restrict direct access to JSP files, only allow access via servlet? Anan Java 8 12-08-2004 11:16 PM
How do I let people access the internet via an access point but not allow them access to my network yar Wireless Networking 4 09-21-2004 03:48 AM
Need help with Style conversion from Style object to Style key/value collection. Ken Varn ASP .Net Building Controls 0 04-26-2004 07:06 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57