How to Change Background Color with Button Click in JavaScript

This is JavaScript Code That will change the background color of button.

changecolor.addEventListener("click", chngcol );

var red1=false;

function chngcol(){

  if(red1)
{
    document.body.style.background="white";
    
red1=false;

  }
else {
    document.body.style.background="red";
    red1=true;
  }


}


function chngcol(){

    document.body.style.backgroundColor= 'rgb('+ Math.floor(Math.random()*256) +', '+ Math.floor(Math.random()*256) +', '+ Math.floor(Math.random()*256) +') ';

}


This is HTML Code

<button id="changecolor">Change Color </button>

Comments