title: Uncaught TypeError: Cannot set property 'background' of undefined
body: <p>I'm about to make a random background color javascript code. So when you refresh the page, all element's background color will change, but in Chrome, I got an error : </p>
  
  <blockquote>
    <p>"Uncaught TypeError: Cannot set property 'background' of undefined"</p>
  </blockquote>
  
  <p>Here's my JavaScript and HTML : </p>
  
  <p><div class="snippet" data-lang="js" data-hide="false">
  <div class="snippet-code">
  <pre class="snippet-code-js lang-js prettyprint-override"><code>var colors = [
      [
          [65], [131], [215]
      ], [
          [217], [30], [24]
      ], [
          [245], [215], [110]
      ], [
          [135], [211], [124]
      ]
  ];
  //Getting a random color 
  var random = Math.floor(Math.random() * 4);
  
  var block = document.getElementsByClassName('block');
  var obj;
  for (obj in block) {
      if (block.hasOwnProperty(obj)) {
  
          block[obj].style.background =
              "rgb" + "(" + colors[random][0] + ", " + colors[random][1] + ", " + colors[random][2] + ")";
      }
  }
  
  
  document.getElementById('body').style.background = "rgb" + "(" + colors[random].r + ", " + colors[random].g + ", " + colors[random].b + ")";</code></pre>
  <pre class="snippet-code-html lang-html prettyprint-override"><code>&lt;a href="#"&gt;
  &lt;li class="block block1"&gt;
   &lt;i class="icon-vcard"&gt;&lt;/i&gt;
   &lt;h3&gt;Contact us&lt;/h3&gt;
   &lt;content&gt;
   &lt;h3&gt;Contact us&lt;/h3&gt;
   &lt;p&gt;
   Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's&lt;/p&gt;
   &lt;/content&gt;
  &lt;/li&gt;
  &lt;/a&gt;
  &lt;a href="#"&gt;
   &lt;li class="block block2"&gt;
   &lt;i class="icon-users"&gt;&lt;/i&gt;
   &lt;h3&gt;Staff&lt;/h3&gt;
   &lt;content&gt;
   &lt;h3&gt;Staff&lt;/h3&gt;
   &lt;p&gt;
   Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's&lt;/p&gt;
   &lt;/content&gt;
  &lt;/li&gt;
  &lt;/a&gt;
  &lt;a href="#"&gt;
   &lt;li class="block block3"&gt;
   &lt;i class="icon-wrench"&gt;&lt;/i&gt;
   &lt;h3&gt;Tools&lt;/h3&gt;
   &lt;content&gt;
   &lt;h3&gt;Tools&lt;/h3&gt;
   &lt;p&gt;
   Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's&lt;/p&gt;
   &lt;/content&gt;
  &lt;/li&gt;
  &lt;/a&gt;
  &lt;a href="#"&gt;
   &lt;li class="block block4"&gt;
   &lt;i class="icon-info"&gt;&lt;/i&gt;
   &lt;h3&gt;About us&lt;/h3&gt;
   &lt;content&gt;
   &lt;h3&gt;About us&lt;/h3&gt;
   &lt;p&gt;
   Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's&lt;/p&gt;
   &lt;/content&gt;
  &lt;/li&gt;
  &lt;/a&gt;
  
  &lt;/ul&gt;
  
                 </code></pre>
  </div>
  </div>
  </p>