How to count LI elements from a list

Hi, I have the next list:

<ul id='myul'>

I would like to know how can I count the LI elements from ‘myul’ ( just a,b,c,d )

But not count b1, b2 and b3, in this case the function should return 4


var ul = document.getElementById("myul");
var liNodes = [];

for (var i = 0; i < ul.childNodes.length; i++) {
	if (ul.childNodes[i].nodeName == "LI") {

liNodes will contain the LI nodes.

Thank you!

Firefox, Safari and Opera supports document.evaluate which makes life easier since you can use XPath to get the nodes you want:

var ul = document.getElementById("myul");
var result = document.evaluate("li", ul, null, XPathResult.ORDERED_NODE_ITERATOR_TYPE, null);

var nextLi;

while (nextLi = result.iterateNext()) {

More elegant and powerful solution than above, but it does not work in IE…


The IE way:

var myUL = document.getElementById("myUL");
var list = myUL.children.tags("LI");