change background color using javascript onclick code example

Example 1: javascript change background color

document.body.style.backgroundColor = "yellow";

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