Thursday, July 24, 2008

IE DOM BUGS : innerHTML='' clears child elements.

I was using ELEMENT.innerHTML ='' to clear the children of a node, so i could layout the same elements after sorting an array - to my suprise when i tried to re-add the children in the correct sort order, they were all empty. Doing the same thing in firefox was ok. It turns out that this is a known bug in IE. So the moral of the story is, if you want to clear the children of a node but want to keep the children for use elsewhere then DON'T use ELEMENT.innerHTML=''. Instead do something like:-

while (ELEMENT.children.length>0) {
ELEMENT.removeChild(ELEMENT.children[0]);
}

Here is more info about this little shocker
http://www.quirksmode.org/js/tests/dom_innerhtml.html

No comments: