Jenscript childNodes does not find all div children when using appendchild

Ok, I hope someone can help me. I apologize to this that this may be misleading. I included an example to try to ease confusion, as it is better seen and then heard.

I created a web page containing a group or set of groups. Each group has a subgroup. In short, what is happening, this page will allow me to combine several groups containing subgroups into a new group. The page will give you the opportunity to rename old subgroups before they are merged into new groups to avoid confusion.

When the group is renamed, it will check to make sure that there is no group with this name yet. If it is copied from its own group to this group, then it will delete the original. If the group does not exist yet, it will create this group, copy itself and delete the original.

Subgroups can also be renamed, and at this moment they will move to a group with the same name if it exists, or create a new one if it is not.

There is a main div on the page. The main div contains the "new subgroups" of the div. Inside each of them is another div containing sections of the "old subgroup". I use a loop through the child nodes of the "new subgroup" div when renaming the group to find each child element of the node. Then they are copied to a new div, basically a div.

The essence of the problem is this. If I go through the DIV and copy all the DIVs in it into a new or existing DIV, everything will be fine. When I try to take this DIV and copy all of its DIVs to another or a new DIV, it always skips one of the moved DIVs.

For simplicity, I copied all the working code below. To recreate the problem, click the place where the image should appear next to the name ewrewrwe and rename it to another. All is well. Now click this new group in the same way and call it something else. You will see that it skips every time.

I linked the page here: http://vtbikenight.com/test.html

The link is clean, this is my personal website that I use for the local motorycle group of which I am a part.

Thanks for helping everyone !!! Please let me know if I can clarify the situation.

I know that the code is not the best right now, it's just a demo code, and I want the concept to work, and then simplify it all.

+4
source share
2 answers

You need to decrease the counter after deleting the node, otherwise you will skip the node, which just replaced the remote node.

for ( var count = 0; count < obj.childNodes.length; count++ ) { if(obj.childNodes[count].tagName == 'DIV'){ //alert(obj.childNodes[count].tagName +" - "+obj.childNodes[count].id); RenameOldSubGroup(obj.childNodes[count].id,NewNewGroupName) count--; // Decrement count to account for node you removed } } 
+4
source

Well, in the loop, you have a counter that you increment, and you pull nodes from the list. That is, obj.childNodes.length will change whenever you pull something out of the container.

+3
source

Source: https://habr.com/ru/post/1307171/


All Articles