Change Background Color onclick using JavaScript

In this tutorial we will see how to Change Background Color onclick using JavaScript. The getComputedStyle() method and backgroundColor property is used for this which will change the background color.

HTML Code

HTML Code is given below, This code contains four span elements with different background colors.

<!DOCTYPE html>
<html>
<head>
<title>Change Background Color onclick using JavaScript</title>
</head>
<body>
 <div class="container">
  <span id='1' onclick="color(this)" class='color clr1'></span>
  <span id='2' onclick="color(this)" class='color clr2'></span>
  <span onclick="color(this)" class='color clr3'></span>
  <span onClick="color(this)" class='color clr4'></span>
 </div>
</body>
</html>

CSS Code

CSS Code is given below, with the help of this code different background colors are assigned to span elements and the main container is centrally aligned.

* {
margin: 0;
padding: 0;
}
body {
display: flex;
height: 100vh;
align-items: center;
justify-content: center;
}
.container {
border: 2px solid #333;
}
.color {
display: table-cell;
width: 50px;
height: 50px;
margin: 0 1px;
cursor: pointer;
border: 2px solid #FFF;
}
.clr1 {
background-color: #9B0FDB;
}
.clr2 {
background-color: #ffea00;
}
.clr3 {
background-color: #04DB53;
}
.clr4 {
background-color: #0F68DB;
}

JavaScript Code

JavaScript Code is given below, In this code we have used getComputedStyle() method and backgroundColor property.

getComputedStyle() method returns the CSS properties and their values of selected element. It is used to read the background color of span elements. style.backgroundColor property returns only inline css properties and their values that is why we have used getComputedStyle() method.

<script>
function color(z) {
var a = getComputedStyle(z);
var b = a.backgroundColor;
document.getElementsByTagName("BODY")[0].style.backgroundColor=b;
}
</script>

Video Tutorial

Watch video tutorial on Change Background Color onclick using JavaScript.

Get input Element Value On Click Using JavaScript Toggle Text of HTML Element with JavaScript onclick Call Multiple Functions on one click using JavaScript onclick Check UnCheck all Checkboxes with JavaScript Get Current Page URL using JavaScript Get ID of Clicked Element using JavaScript How To Delay JavaScript Function Call Change Background Color onclick using JavaScript Delete HTML Element using JavaScript Change HTML Attribute Value using JavaScript Show and Hide Element On Click Using JavaScript Passing JavaScript Value from one Page to another T-Rex Run Game Hacked - Hack T Rex Run Game How To Use JavaScript Function as Variable Display Current Date and Time Using HTML and JAVASCRIPT Get Unicode Value of Pressed Keyboard Key using JavaScript Detect Enter Key Press Using JavaScript Hide and Show Element on Enter Key Press Get Mouse Cursor Position in Pixels using JavaScript Open Link in New Window using JavaScript Open Link in New Tab using JavaScript Get Image Width and Height using JavaScript Remove Last Digit of Number using JavaScript Bitwise OR Simple JavaScript Calculator Using Vanilla JavaScript Disable Right Click on Website Using JavaScript How To Add Line Breaks in JavaScript Alert Box