Why is Javascript not working?

  • 0
    let button = document.querySelector('.button');
    let divBg = document.querySelector('.box1');
    
      function changeBackground(color) {
        divBg.style.background = color;
     }
     
     button.addEventListener("click",function() { changeBackground('black') });


    Throws null on addEventListener, newbie himself. If it is not difficult for you, you can explain in detail what the error is)
    JavaScript Anonymous, Mar 19, 2020

  • 2 Answers
  • 0
    The selector is not correct.

    https://learn.javascript.ru/css-scoping
    William Hebert

  • 0
    scripts need to be added at the end of the body, maybe the problem is that the elements that you are trying to use have not loaded
    Anonymous

Your Answer
To place the code, please use CodePen or similar tool. Thanks you!