title: Why is e.target.style.backgroundColor not updating when the color has been defined outside of the function?
body: <p>This is an etch a sketch project I'm trying to do, I got the two primary functions working which is the greyscale (each pass gets darker) and rainbow which outputs a random color on hover</p>
  
  <p>I wanted to add preset colors next but for some reason it isn't updating. I've tried everything I can think off, like adding the value to an array that only keeps the current value, to setting <code>e.target.style.backgroundColor = className[0];</code> which works. </p>
  
  <p>The problem is I want to use my own shades of the colors not the predefined css color names (I'm aware the rgb values I have are the same as default ones, but that's temporary).</p>
  
  <p>I've figured that if I assign a value to currentColor inside of the updateColor() function that it works but of course looses all of the flexibility.</p>
  
  <p>I've put a console.log() inside of the function to see if it was receveing the value and it is but for some reason it isn't updating at all.</p>
  
  <p>PS: I apologize in advance for the messy code</p>
  
  <p>Edit: edited into a Minimal, Complete, and Verifiable example</p>
  
  <p><div class="snippet" data-lang="js" data-hide="false" data-console="false" data-babel="false">
  <div class="snippet-code">
  <pre class="snippet-code-js lang-js prettyprint-override"><code>const grid = document.querySelector(".grid");
  const clear = document.querySelector("#clear");
  clear.addEventListener("click", reset);
  function createDivs(){
      for(i = 1; i &lt; 16 * 16 + 1; i++){
          let createdDivs = document.createElement("div")
          grid.appendChild(createdDivs)
          createdDivs.setAttribute("class", "grid-element" )
      }
      grid.style.gridTemplateColumns = `repeat(${16}, 1fr)`
      grid.style.gridTemplateRows = `repeat(${16}, 1fr)`
  }
  createDivs();
  function reset(){
      let divs = document.querySelectorAll(".grid-element")
      divs.forEach(function(div){
          div.parentNode.removeChild(div);
      });
      createDivs();
  }
  
  const li = document.querySelectorAll("ul li")
  li.forEach(function(e){
      e.addEventListener("click", fetchClassName)  
  })
  let className = ["black"]
  let oldClassName = ["black"]
  let currentColor = "black"
  function fetchClassName(e){
      className.unshift(e.srcElement.className);
      oldClassName = className.pop();
      setColor();
  }
  function setColor(){
      let divs = document.querySelectorAll(".grid-element");
      switch(className[0]){
          case "rainbow":
              divs.forEach(function(div){
                  div.removeEventListener("mouseover", updateColor);
                  div.addEventListener("mouseover", skittles);
              })
              break;
          case "blue":
              currentColor = "rbga(0, 0, 255, 1)"
              for (y = 0; y &lt; divs.length; y++){
                  let div = divs[y];
                  div.removeEventListener("mouseover", skittles);
                  div.addEventListener("mouseover", updateColor);
              }
              break;
      }
  }
  function updateColor(e){
          e.target.style.backgroundColor = currentColor //className[0] works but not what I want
          console.log(currentColor)
  }
  function skittles(e){
          let r = Math.floor(Math.random() * 256);
          let g = Math.floor(Math.random() * 256);
          let b = Math.floor(Math.random() * 256);
          let rgb = `rgba(${r}, ${g}, ${b}, 0.5)`
          e.target.style.backgroundColor = rgb;
  }</code></pre>
  <pre class="snippet-code-css lang-css prettyprint-override"><code>.grid {
    display: grid;
    height: 550px;
    width: 550px;
  }
  .grid-element {
    grid-area: auto;
    border-bottom: 1px solid rgba(0, 0, 0, 0.1);
    border-left: 1px solid rgba(0, 0, 0, 0.1);
  }
  ul li:hover {
    cursor: pointer;
  }</code></pre>
  <pre class="snippet-code-html lang-html prettyprint-override"><code>&lt;div class="container"&gt;
      &lt;div class="grid"&gt;&lt;/div&gt;
  &lt;/div&gt;
  &lt;div&gt;
      &lt;button type="button" id="clear"&gt;Clear Canvas&lt;/button&gt;
          &lt;ul&gt;
              &lt;li class="blue"&gt;blue&lt;/li&gt;
              &lt;li class="rainbow"&gt;rainbow&lt;/li&gt;
          &lt;/ul&gt;
   &lt;/div&gt;</code></pre>
  </div>
  </div>
  </p>