0

Is there a way to allow me to iterate through an ordered list similar in functionality as table.rows? I am using Javascript.

The reason I ask this is because I have an ordered list that contains another ordered list inside. I just want to return the parent list items and not the child.

<ol id="pList">
    <li>Item A</li>
    <li>
        <ol id="cList">
            <li>A's Child 1</li>
            <li>A's Child 2</li>
        </ol>
    </li>
    <li>Item B</li>
    <li>Item C</li>
    <li>Item D</li>
</ol>

I now use getElementsbyTag and that returns all li's including the one in cList. I just want the ones in pList.

Thanks

FYI: I would prefer it done in Javascript. The code has to work within Greasemonkey. I don't really know much about jquery and I am not really much of a javascript programmer.

4
  • Are you asking just for vanilla Javascript or you can use jQuery? Commented Feb 14, 2013 at 15:21
  • you want 'plain' javascript or you can use jquery? Commented Feb 14, 2013 at 15:22
  • 7
    Just for fun, let's assume that a question that's not tagged jquery doesn't want a jQuery answer. Commented Feb 14, 2013 at 15:22
  • Javascript. I don't know much about jquery and also it has to work in Greasemonkey. Commented Feb 14, 2013 at 15:23

3 Answers 3

5

There is no specific property that gives you direct access to the <li> children of an <ol>.

However it is very easy to enumerate them, particularly when you consider that the only legal children of an <ol> must be white space text nodes and <li> nodes.

var ol = document.getElementById('pList');
var li = [];
var node = ol.firstChild;
while (node) {
    if (node.tagType === 3 && node.tagName === 'LI') {
        li.push(node);
    }
    node = node.nextSibling;
}

At the end of which, the array li contains the required children.

The above code will work in any browser. The .children collection has problems on older versions of MSIE, and querySelectorAll is even more modern (and therefore less widely supported).

Sign up to request clarification or add additional context in comments.

4 Comments

What about var li = document.getElementById('pList').children;
@Ghommey .children has "odd" behaviour on older versions of MSIE. See stackoverflow.com/questions/7935689/…
And .childNodes? Or are we referring to the same thing here?
I was hoping there was an iterator. Guess this is as good as any
1

If using querySelectorAll()* is an option for you, it's easy:

var listItems = document.querySelectorAll( '#pList > li' );

* note: it's a native DOM method, and has nothing to do with jQuery

1 Comment

Nice. Will use it next time as the OLs do not always have ids in this case. Thanks
0

There's a table of immediate children in every javascript object :

document.getElementById('pList').children

You can even iterate through it and check whatever your need :

var el = document.getElementById('pList');
for (var i = 0; i < el.children.length; i++) {
    if (el.children[i].tagName == "LI") {
        el.children[i].doWhatever();
    }
}

2 Comments

I was hoping there was direct access like table.rows. So I guess old fashioned way will do.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.