I get an array of DOM objects using getElementsByClassName:
a = document.getElementsByClassName("foo");
At this point, suppose a.length is 3. Then, during some function call, I want to switch the attributes of some of these objects by changing their class names. For example, I modify the class name of a[0]:
a[0].className = "bar";
It seems that the object that was just called as a[0] is now removed from a. a.length is now 2.
I am wondering why this happened. I defined a for once, but later modifications to its elements seem to be affecting the memberships of a. Can someone explain this?
if you collect the elements with querySelectorAll, the object that contains them will not be live, and represents a snapshot of the webpage. You could also remove the ‘liveness’ by turning the node list into an ordinary array-