javascript - How to correctly iterate through getElementsByClassName -


i javascript beginner.

i initing web page via window.onload, have find bunch of elements class name (slide) , redistribute them different nodes based on logic. have function distribute(element) takes element input , distribution. want (as outlined example here or here):

var slides = getelementsbyclassname("slide"); for(var = 0; < slides.length; i++) {    distribute(slides[i]); } 

however not magic me, because getelementsbyclassname not return array, nodelist, is...

...this speculation...

...being changed inside function distribute (the dom tree being changed inside function, , cloning of nodes happen). for-each loop structure not either.

the variable slides act's un-deterministicaly, through every iteration changes it's length , order of elements wildly.

what correct way iterate through nodelist in case? thinking filling temporary array, not sure how that...

edit:

important fact forgot mention there might 1 slide inside another, changes slides variable have found out user alohci.

the solution me clone each element array first , pass array ono-by-one distribute() afterwards.

according mdn, way retrieve item nodelist is:

nodeitem = nodelist.item(index) 

thus:

var slides = document.getelementsbyclassname("slide"); for(var = 0; < slides.length; i++) {    distribute(slides.item(i)); } 

i haven't tried myself (the normal for loop has worked me), give shot.


Comments

Popular posts from this blog

javascript - jQuery: Add class depending on URL in the best way -

caching - How to check if a url path exists in the service worker cache -

Redirect to a HTTPS version using .htaccess -