I have tried to use several methods to do this, but the onclick function is not responding. I use chrome. Can you please respond very clearly because I am a beginner.
<html>
  <head>
    <style>
      .a{
        display:inline;
        background-color: blue;
        color: blue;
        width: 100px;
        float: left;
      }
      .b{
        display:inline;
        background-color: red;
        color: red;
        width: 100px;
        float: left;
      }
      h1:hover{
        background-color: white!important;
        color: white!important;
      }
      .a::selection{
        display:inline;
        background-color: blue;
        color: blue;
        width: 100px;
        float: left;
      }
      .b::selection{
        display:inline;
        background-color: red;
        color: red;
        width: 100px;
        float: left;
      }
    </style>
  </head>
  <body>
    <div id="div" class="div">
    </div>
    <script>
      for(var i = 1;i<10;i){
        var a = document.createElement('h1')
        document.getElementById('div').appendChild(a)
        a.innerHTML=("HI")
        a.id="id"
        document.getElementById("id").setAttribute('class', 'a');
        a.id=null
        a.href=""
        i=i+1
        var b = document.createElement('h1')
        document.getElementById('div').appendChild(b)
        b.innerHTML=("BYE")
        b.id="id"
        document.getElementById("id").setAttribute('class', 'b');
        b.id=null
        b.href=""
        i=i+1
        }
        for(i=1;i<10000;i){
          setTimeout(function(){
            document.getElementsByClassName('a')[0].setAttribute("style","background-color:red;color:red")
            Array.prototype.forEach.call(document.getElementsByClassName('a'),
                             item => item.setAttribute("style","color: blue;background-color:blue"));
          },(2*i-0)*1000)
          setTimeout(function(){
            document.getElementsByClassName('a')[0].setAttribute("style","background-color:blue;color:blue")
            Array.prototype.forEach.call(document.getElementsByClassName('a'),
                             item => item.setAttribute("style","color: red;background-color:red"));
          },(2*i-1)*1000)
          setTimeout(function(){
            document.getElementsByClassName('b')[0].setAttribute("style","background-color:blue;color:blue")
            Array.prototype.forEach.call(document.getElementsByClassName('b'),
                             item => item.setAttribute("style","color: blue;background-color:blue"));
          },(2*i-1)*1000)
          setTimeout(function(){
            document.getElementsByClassName('b')[0].setAttribute("style","background-color:red;color:red")
            Array.prototype.forEach.call(document.getElementsByClassName('b'),
                             item => item.setAttribute("style","color: red;background-color:red"));
          },(2*i-0)*1000)
          i=i+1
        }
        var color = document.querySelectorAll('h1')
        color.onclick = function(){
          alert('debug')
          if (this.style.backgroundColor == 'white') {
            this.style.backgroundColor = '#000001'
            this.style.color = '#000001'
          }
          
          if (this.style.backgroundColor == '#000001') {
            this.style.backgroundColor = 'white'
            this.style.color = 'white'
          }
        }
    </script>
  </body>
</html>The other parts work but not the onclick part. Can you please help? It does not show any error in chrome. The snippet above is actually all my code, and I wish to be informed of what error this is and how to solve it.
 
    