Element.childNodes returns all types of nodes, including text fields as spaces. Perhaps this is not what you want. If you only care about the number of children, use childElementCount .
var b = xmlDoc.getElementsByTagName("B")[0]; alert(b.childElementCount);
I have not tried in IE, it may not work. Otherwise, if you need a list of elements, use children children , which are not supported in a non-HTML document. You can try this function:
function getChildren(element) { var nodes = element.childNodes; var children = []; for (var i = 0; i < nodes.length; i++) { if (nodes[i].nodeType == Node.ELEMENT_NODE) children.push(nodes[i]); } return children; } getChildren(b).length;
syockit
source share