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

    <ul id"2">

    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, May 17, 2005
    1. Advertisements

  2. abs

    RobB Guest

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

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

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

    RobB, May 17, 2005
    1. Advertisements

  3. abs

    RobG Guest

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

    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.
    RobG, May 17, 2005
  4. abs

    RobB Guest


    Eesh...early morning here. Never mind.
    RobB, May 17, 2005
    1. Advertisements

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments (here). After that, you can post your question and our members will help you out.