Endless loop through additional DOM element
Without using the formula for using XXX
This question is not meant to find a practical solution through a framework. Answer using frame XXX, or is it that simple in frame XXX, or why not use this XXX frame ??? does not answer the question.
I have a feature designed to run after the page load
. This function iterates over all DOM elements that are tags
, checks to see if they have an
, and if so, calls it
, passing it the reference of the matched element.
My goal was to add another
to the element that is being passed to
With the code I've written so far, I can add to the parent just fine. However, if I comment out the line
and instead try the prefix line the browser is hanging in a supposedly infinite loop.
It is not easy for me to understand why this is so.
source to share
(for example, the returned one
) is usually a live list - changes made to the DOM are also reflected in it. So every time you add
before the current, you expand the list with one element and move the current element by one, and the next iteration takes you back to the node you just finished.
You have some easy workarounds for this ...
Kill the counter when you add node.(Awful, and if you ever add something instead
, you end up missing nodes and it won't be obvious why.)
Copy list to array and iterate over the array. You can do it with something like
children = .slice.call(children, 0);
(more often) or
children = Array.apply(window, children);
one that returns you a NodeList, which won't work. (And even if it was live, in which case you could choose
, and the inserted gaps did not appear in this case.)
Iterations backward (from
children.length - 1
source to share