I'm having a seemingly dumb problem, but since I'm not very knowledgeble at JavaScript, I need some help.
I'm just trying to iterate through an array, but it seems to be skipping elements for no reason. My function is really simple:
function uncolour(classname)
{
var anchors = document.getElementsByClassName(classname);
for(var i = 0; i < anchors.length; i++) {
var anchor = anchors[i];
anchor.classList.toggle("nocolor");
}
}
The problem is that it is consistently skipping an element. It works for the first element, then for the third and so on. I have verified and all the correct elements are present in the anchors array, but it toggles the class only on the even indexes. Any idea about why? I am at a loss here.
EDIT: Thank you for your responses. I've read the other similar post, but the case with me is that toggling the "nocolor" class shouldn't affect the elements of the array, because the classname that I'm looking for is not the same as "nocolor". I think that while the elements may remain the same, they are somehow rearragend because I've changed the class of an element in the document. So, I don't know why, but that worked for me: for(var i = anchors.length-1; i>=0; i--)