The problem is that document.getElementsByTagName()
returns a NodeList
, not an Array
. The content, and therefore length, of a NodeList
is updated when you remove an element from the DOM that's in the NodeList
. So when you remove the first element, the NodeList
gets shorter and a new first element occupies index
0. Updating index
in the loop therefore makes you miss at least one element, possibly more depending on the length of the result.
Try something like this:
var elements = document.getElementsByTagName('label')
while (elements[0]) elements[0].parentNode.removeChild(elements[0])