I am writing something academic where I have namespaced HTML elements like:
<ns:LinkList id="sitesuteis" cssClass="classone">
<ns:LinkItem id="LI1" href="http://www.ibt.pt/" target="_blank">IBT</ns:LinkItem>
<ns:LinkItem id="LI2" href="http://http://html5demos.com/t/" target="_blank">HTML5 Demos</ns:LinkItem>
<ns:LinkItem id="LI3" href="http://diveintohtml5.ep.io/" target="_blank">Dive into HTML5</ns:LinkItem>
<ns:LinkItem id="LI4" href="http://html5boilerplate.com/" target="_blank">HTML5 Boilerplate</ns:LinkItem>
</ns:LinkList>
Now, in JavaScript I am trying:
var elements = document.getElementsByTagName('ns:LinkItem');
element = elements[0];
console.log(element.getAttribute('id'));
//I get a correct value in all browsers
to get all the ChildNodes in my elements[0]
. It works fine in all browsers, except -IE lt 9-
I tried:
var children = element.getElementsByTagName('ns:LinkItem');
console.log(children.length);
and:
var children = Array();
for (i=0; i<element.childNodes.length; i++){
alert(element.childNodes[i].nodeName);
if (element.childNodes[i].nodeName=="NS:LINKITEM"){
children.push(element.childNodes[i]);
}
}
console.log(children.length);
In both console.logs, I get the correct length (4) in every browser except Internet Explorer 8 or less where I get 0.
According to @Shadow Wizard, in Internet Explorer 8 and below, the canHaveChildren
property of the element is false which means dead end, the browser simply doesn't support having child nodes for this tag, same way that <br />
can't have child nodes for example. I have tried it and it is true. If I try:
element.parentNode
in Internet Explorer 8 or less, I get the div
that contains my markup and in the other browsers I get my parent <ns:LinkList>
.
I really need a hack for this, and I can't seem to find one.