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.

JavaScript Hide Button After Click Simple Click and Hold Effect Using JavaScript JavaScript Select All HTML Elements Whose ID Start With Same String Count Checked Checkboxes of Specific Form with JavaScript Enable and Disable Button using JavaScript Add Class to HTML Tag On Click using JavaScript Change Class of HTML Tag Using JavaScript Get All Options In Select Tag using JavaScript Replace a Word from everywhere in a String using JavaScript Execute a Function on Hover Using JavaScript Open Select File Dialog Box Using JavaScript Display Message When File Is Selected Using JavaScript Get Clicked HTML Element Name Using JavaScript Get Class of Clicked Element Using JavaScript Count Number of Rows in Table using JavaScript Move Background Image with Mouse Using JavaScript Disable Text Selection Using JavaScript Get Selected Radio Button Value On Click using JavaScript Get input Element Value On Click Using JavaScript Toggle Text of HTML Element with JavaScript onclick