0

My code is:

var isBackgroundApplied = 0;
var button = document.getElementsByClassName("change");

button.addEventListener("click",function(){
 if(isBackgroundApplied === 0){
     document.querySelector("body").style.color = "blue";
     isBackgroundApplied = 1;
 }else{
    document.querySelector("body").style.color = "white";
    isBackgroundApplied == 0;
 }
});

Please help me, the task for the code is to change background color on click.

  • Did you by any chance put this code before the HTML body? – Jeremy Thille Feb 08 '21 at 16:27
  • It is right at the end of the body,the script reference – Coding Experience Feb 08 '21 at 16:36
  • 1
    See the duplicate - `getElementsByClassName` returns a list (which is clear from any [documentation](https://developer.mozilla.org/en-US/docs/Web/API/Document/getElementsByClassName) you might care to read...). You can't add an event listener to a list (because it's not a HTML element). You need to loop through the list and add the listener to each one individually. – ADyson Feb 08 '21 at 16:37

0 Answers0