In Javascript, I get the elements of a given class using:
A = document.getElementsByClassName("someclass");
> A contains elements e1, e2, e3, e4
Then, I change the class of one of these elements:
document.getElementById("e2").className = "anotherclass";
To my great surprise, the array A has been automatically changed in the process !!
> A contains elements e1, e3, e4
I thought that the array returned by getElementsByClassName
would stay the same now that is was assigned.
- How is that possible? Is it an intended behaviour?
- Is there a simple way to change that?
Here is a JSfiddle.
Note that this is also true for getElementById
.