title: How to change background color after using 'onkeydown'?
body: <p>I was trying many solutions but none worked.
  The problem is where I put the comment.
  I trying to do a game which I can change a color of boxes using arrows.</p>
  
  <pre><code>var p = 0;
  var d0 = document.getElementById('p1').getAttribute('value');
  var d1 = document.getElementById('p2').getAttribute('value');
  var arraj = [];
  arraj.push(d0, d1);
  
  function change() {
    for (var i = 0; i &lt; arraj.length; i++) {
      if (arraj[i] == p) {
  
        // and here is the problem
        arraj[i].style.backgroundColor = "red";
      }
    }
  }
  
  document.onkeydown = check;
  
  function check(e) {
    e = e || window.event;
    if (e.keyCode == '37') {
  
      // left
    } else if (e.keyCode == '39') {
      p++;
      change();
      // right
    }
  }
  
  </code></pre>
  
  <p>and HTML</p>
  
  <pre><code>    &lt;div class='g' id='p1' value="0"&gt;bla&lt;/div&gt;
      &lt;div class='g' id='p2' value="1"&gt;bla&lt;/div&gt;
  </code></pre>