Reputation: 14286
I am trying to access child nodes of a root element but I am unable to do so as the elements that come back are all "undefined" - including the array that is returned.
<html>
<head>
<script>
function dothis()
{
var elements = document.getElementsByTagName("body").parentNode.childNodes;
alert(elements.length);
}
</script>
</head>
<body onload="dothis();">
<p>Welcome</p>
<ul>
<li>hello</li>
</ul>
</body>
</html>
Upvotes: 0
Views: 1296
Reputation: 165941
getElementsByTagName
returns a NodeList
(according to the spec. In some browsers e.g. Firefox I think it returns an HTMLCollection
). In any case, what it returns is an array-like object. You need to access the element at a specific index:
var elements = document.getElementsByTagName("body")[0].parentNode.childNodes;
Upvotes: 1