title: Javascript - Adding class to element from array
body: <p>I would like to add classes to an element in order from an array. I have three different classes, and I am not sure how to loop back to the beginning of the array to start over when I get to the end of the array of classes. Here is what I have so far:</p>
  
  <pre><code>var backgrounds = ["gray", "red", "blue"];
  var elements = document.getElementsByClassName("blogpost");
  var x = 0;
  for (i = 0; i &lt; elements.length; i++) {
      elements[i].classname += backgrounds[i];  //This is where I do not know how to add the elements in order
      x++; //This will get to the end of the array, how do I loop back to the beginning of the array after hitting the last element?
  }
  </code></pre>