title: How to change the CSS of an HTML element with the output of a Javascript function?
body: <p>I would like to make a page <a href="https://stackoverflow.com/questions/2777479/display-random-image-when-page-loads-without-utilizing-onload-in-the-body-tag" title="load a random image">display a random image on load like this</a> in the future, but instead of returning a "<strong>&lt; img src="image.png" ></strong>" to be added with "<strong>document.write(imageTag())</strong>", I would like to use a <strong>setAttribute()</strong> or <strong>element.style.backgroundImage()</strong> to change the CSS of a div block I have set aside with the output of a random image Javascript function. 
  <hr>
  For instance, I am trying to change the background-color of a simple div block, in order to "change the css with the output of a Javascript function." (original question)</p>
  
  <p>html:</p>
  
  <pre><code>&lt;script&gt;
    document.getElementById("coloredBlock").style.backgroundColor = color();
    &lt;/script&gt;
  </code></pre>
  
  <p>CSS:</p>
  
  <pre><code>#coloredBlock {
    background-color: yellow;
    height: 100px;
    width: 100px;
  } 
  </code></pre>
  
  <p>Javascript:</p>
  
  <pre><code>function color(){
  return "purple";
  }
  </code></pre>
  
  <p>The output I am looking for is for that block to be purple. A <a href="https://codepen.io/anon/pen/qyVGGw" rel="nofollow noreferrer" title="pen">codepen</a> of what I mean.
  <hr>
  Also, my setAttribute() doesn't seem to work, only element.style:</p>
  
  <pre><code>&lt;script&gt;
  document.getElementById("coloredBlock").style.backgroundColor = "green";
    &lt;/script&gt;
  </code></pre>
  
  <p>works and the block turns green, but</p>
  
  <pre><code>&lt;script&gt;
  document.getElementById("coloredBlock").setAttribute('background-color', 'blue')
    &lt;/script&gt;
  </code></pre>
  
  <p>does not work and the block remains yellow.</p>
  
  <hr>
  
  <p>I am certain the answer has something to do with how  "  document.getElementById("coloredBlock").style.backgroundColor = color();
  " is written.</p>