change background color with button javascript code example

Example 1: javascript change color of button

document.getElementById("button").style.background='#000000';

Example 2: javascript change background color

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

Example 3: JavaScript Change Background Color

var element = document.body;
element.style.backgroundColor = "blue";

Example 4: 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>