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

Discussion in 'Javascript' started by abs, May 17, 2005.

  1. abs

    abs Guest

    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
    abs, May 17, 2005
    #1
    1. Advertising

  2. abs

    RobB Guest

    abs wrote:
    > 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.


    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/programming/javascript/definitive/chap17/7.html
    RobB, May 17, 2005
    #2
    1. Advertising

  3. abs

    RobG Guest

    abs wrote:
    > 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">


    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>

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


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


    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.parentNode.nodeName) {
    alert('found ' + uls.id);
    }
    }
    ">

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


    --
    Rob
    RobG, May 17, 2005
    #3
  4. abs

    RobB Guest

    RobB wrote:
    > abs wrote:
    > > 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.

    >
    > 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/programming/javascript/definitive/chap17/7.html

    Eesh...early morning here. Never mind.
    RobB, May 17, 2005
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Nathan Sokalski
    Replies:
    0
    Views:
    718
    Nathan Sokalski
    May 30, 2005
  2. Thomas Guettler

    Emacs mode: Display higher level

    Thomas Guettler, Nov 5, 2003, in forum: Python
    Replies:
    2
    Views:
    347
    Steve Evans
    Nov 6, 2003
  3. Leon

    Higher-level OpenGL modules

    Leon, Sep 5, 2006, in forum: Python
    Replies:
    5
    Views:
    478
    Ben Sizer
    Sep 6, 2006
  4. Iain
    Replies:
    7
    Views:
    536
    John Dunlop
    Oct 17, 2006
  5. pabbu
    Replies:
    8
    Views:
    708
    Marc Boyer
    Nov 7, 2005
Loading...

Share This Page