I am trying to make following piece of code to work for each child node once. THe function is also deleting the node as per logic, for more than one child node it never seems to traverse to each child node.
//Deleting from child node
var target =document.getElementById(element.name).childNodes[0];
if(target.hasChildNodes())
{
var children = new Array();
children = target.childNodes;
for(child in children)
{
if(children[child].tagName == 'DIV'){
//target.removeChild[child];
var deleteChild = document.getElementById(target.childNodes[child].id);
deleteChild.parentNode.removeChild(deleteChild);
}
}
}
In a special case i have 4 "Div" as child, this only remove two DIV and not all. I assume as the length is also changing constantly, hence it's not able to get to all children.
Is this correct way of traversal, am i missing something obvious?
<>button in the editor and SHOW what you have - add HTML and you have a minimal reproducible example - remember to have output and EXPECTED outputchildNodes, you are also collecting textNodes, whitespaces, and even comments. Can't really tell because you haven't posted any HTML, and you are talking about traversing the DOM...that's like explaining geography without a map.