Change background color on button click in JavaScript W3Schools code example

Example 1: javascript change background color

function changeBackground(color) {
   document.body.style.background = color;
}

window.addEventListener("load",function() { changeBackground('red') });

Example 2: change background color on button click in javascript

<style>
  .bg1 {
  	background-color: red;
  }
</style>

<button id="btnChangebgColor" />

<script>
  	$(document).ready(function(){
      $("#btnChangebgColor").click(function() {
        $(this).addClass(".bg1");
      }); 
	}
</script>

Example 3: how to change background color using js

var warning = document.getElementById("warning");
  warning.style.background-color = "red";

Tags:

Css Example