title: How to correctly iterate through getElementsByClassName
body: <p>I am Javascript beginner.</p>
  
  <p>I am initing web page via the <code>window.onload</code>, I have to find bunch of elements by their class name (<code>slide</code>) and redistribute them into different nodes based on some logic. I have function <code>Distribute(element)</code> which takes an element as input and does the distribution. I want to do something like this (as outlined for example <a href="https://stackoverflow.com/questions/5179798/javascript-getelementsbyclassname-always-returns-none">here</a> or <a href="https://stackoverflow.com/questions/3871547/js-iterating-over-result-of-getelementsbyclassname-using-array-foreach">here</a>):</p>
  
  <pre><code>var slides = getElementsByClassName("slide");
  for(var i = 0; i &lt; slides.length; i++)
  {
     Distribute(slides[i]);
  }
  </code></pre>
  
  <p>however this does not do the magic for me, because <code>getElementsByClassName</code> does not actually return array, but a <a href="https://developer.mozilla.org/en-US/docs/DOM/NodeList" rel="noreferrer"><code>NodeList</code></a>, which is... </p>
  
  <p><strong>...this is my speculation...</strong></p>
  
  <p>...being changed inside function <code>Distribute</code> (the DOM tree is being changed inside this function, and cloning of certain nodes happen). <code>For-each</code> loop structure does not help either.</p>
  
  <p>The variable slides act's really un-deterministicaly, through every iteration it changes it's length and order of elements wildly.</p>
  
  <p>What is the correct way to iterate through NodeList in my case? I was thinking about filling some temporary array, but am not sure how to do that...</p>
  
  <h2><strong>EDIT:</strong></h2>
  
  <p>important fact I forgot to mention is that there might be one slide inside another, this is actually what changes the <code>slides</code> variable as I have just found out thanks to user <a href="https://stackoverflow.com/users/42585/alohci">Alohci</a>.</p>
  
  <p>The solution for me was to clone each element into an array first and pass the array ono-by-one into <code>Distribute()</code> afterwards.</p>