Connecting Tech Pros Worldwide Help | Site Map

<ul>s nested in <li>s - how to get the on on higher level ?

  #1  
Old July 23rd, 2005, 08:51 PM
abs
Guest
 
Posts: n/a
Anybody has an idea how to get the <ul> element which is not nested in <li>
element ? In other words I have several lists like this:

<ul id="1">
<li>Aaaaaaaa</li>
<li>Bbbbbbbb</li>
<li>Cccccccc
<ul>
<li>111111</li>
<li>222222</li>
<li>333333
<ul>
<li>@@@@@@@@@</li>
<li>{{{{{{{}</li>
<li>????>>>>></li>
</ul>
</li>
</ul>
</li>
</ul>

<ul id"2">
<li>qqq</li>
<li>vvvv</li>
</ul>

and I want to get the <ul>s which have id "1" and "2" in this example but I
can't use ids to get them.

Best regards,
ABS


  #2  
Old July 23rd, 2005, 08:52 PM
RobB
Guest
 
Posts: n/a

re: <ul>s nested in <li>s - how to get the on on higher level ?


abs wrote:[color=blue]
> Anybody has an idea how to get the <ul> element which is not nested[/color]
in <li>[color=blue]
> element ? In other words I have several lists like this:
>
> <ul id="1">
> <li>Aaaaaaaa</li>
> <li>Bbbbbbbb</li>
> <li>Cccccccc
> <ul>
> <li>111111</li>
> <li>222222</li>
> <li>333333
> <ul>
> <li>@@@@@@@@@</li>
> <li>{{{{{{{}</li>
> <li>????>>>>></li>
> </ul>
> </li>
> </ul>
> </li>
> </ul>
>
> <ul id"2">
> <li>qqq</li>
> <li>vvvv</li>
> </ul>
>
> and I want to get the <ul>s which have id "1" and "2" in this example[/color]
but I[color=blue]
> can't use ids to get them.[/color]

Hmm...very mysterious. Oh, well...any chance we could see the rest of
your document structure - not just that subtree?

var i = 0,
ul,
unnested_uls = [],
all_uls = document.getElementsByTagName('ul');
while (ul = all_uls.item(i++))
if (null == ul.getElementsByTagName('ul'))
unnested_uls.push(ul);

....should get you a collection of them.

http://www.webreference.com/programm.../chap17/7.html

  #3  
Old July 23rd, 2005, 08:52 PM
RobG
Guest
 
Posts: n/a

re: <ul>s nested in <li>s - how to get the on on higher level ?


abs wrote:[color=blue]
> Anybody has an idea how to get the <ul> element which is not nested in <li>
> element ? In other words I have several lists like this:
>
> <ul id="1">[/color]

An id should start with a letter, though it may contain numbers and
some other characters.

<URL:http://www.w3.org/TR/html401/types.html#type-name>
[color=blue]
> <li>Aaaaaaaa</li>
> <li>Bbbbbbbb</li>
> <li>Cccccccc
> <ul>
> <li>111111</li>
> <li>222222</li>
> <li>333333
> <ul>
> <li>@@@@@@@@@</li>
> <li>{{{{{{{}</li>
> <li>????>>>>></li>
> </ul>
> </li>
> </ul>
> </li>
> </ul>
>
> <ul id"2">[/color]

<ul id="2">
[color=blue]
> <li>qqq</li>
> <li>vvvv</li>
> </ul>
>
> and I want to get the <ul>s which have id "1" and "2" in this example but I
> can't use ids to get them.
>
> Best regards,
> ABS
>
>[/color]

Add this button to your page:

<input type="button" value="Click me" onclick="
var uls = document.getElementsByTagName('ul');
var i = uls.length;
while (i--){
if ( 'LI' != uls[i].parentNode.nodeName) {
alert('found ' + uls[i].id);
}
}
">

If you don't want to use getElementsByTagName, you could just walk
down the DOM tree, but that is getting really silly.


--
Rob
  #4  
Old July 23rd, 2005, 08:52 PM
RobB
Guest
 
Posts: n/a

re: <ul>s nested in <li>s - how to get the on on higher level ?


RobB wrote:[color=blue]
> abs wrote:[color=green]
> > Anybody has an idea how to get the <ul> element which is not nested[/color]
> in <li>[color=green]
> > element ? In other words I have several lists like this:
> >
> > <ul id="1">
> > <li>Aaaaaaaa</li>
> > <li>Bbbbbbbb</li>
> > <li>Cccccccc
> > <ul>
> > <li>111111</li>
> > <li>222222</li>
> > <li>333333
> > <ul>
> > <li>@@@@@@@@@</li>
> > <li>{{{{{{{}</li>
> > <li>????>>>>></li>
> > </ul>
> > </li>
> > </ul>
> > </li>
> > </ul>
> >
> > <ul id"2">
> > <li>qqq</li>
> > <li>vvvv</li>
> > </ul>
> >
> > and I want to get the <ul>s which have id "1" and "2" in this[/color][/color]
example[color=blue]
> but I[color=green]
> > can't use ids to get them.[/color]
>
> Hmm...very mysterious. Oh, well...any chance we could see the rest of
> your document structure - not just that subtree?
>
> var i = 0,
> ul,
> unnested_uls = [],
> all_uls = document.getElementsByTagName('ul');
> while (ul = all_uls.item(i++))
> if (null == ul.getElementsByTagName('ul'))
> unnested_uls.push(ul);
>
> ...should get you a collection of them.
>
>[/color]
http://www.webreference.com/programm.../chap17/7.html

Eesh...early morning here. Never mind.

  #5  
Old July 23rd, 2005, 08:52 PM
abs
Guest
 
Posts: n/a

re: <ul>s nested in <li>s - how to get the on on higher level ?


Hi!

RobG wrote:[color=blue]
> An id should start with a letter, though it may contain numbers and
> some other characters.[/color]

Understood. But it was just an example :) I never use numeric ids :)
[color=blue]
> Add this button to your page: [...]
> If you don't want to use getElementsByTagName, [...][/color]

That's fine, I can use it. Thank you a lot for your help, I'll check it now
!

Best regards,
ABS


Closed Thread