You can probably make this loop simpler by iterating over the child nodes backwards. `for(var k = container.childNodes.length; k >= 0; --k)`. Then, `removeChild` won't affect the rest of the array.