<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Title</title>
<style>
div{
width: 100px;
height: 100px;
transition: 1s;
}
</style>
</head>
<body>
<div id="box1" style="background-color: red;"></div>
<div id="box2" style="background-color: blue;"></div>
<div style="background-color: yellow;"></div>
<script>
var col =[];
document.querySelectorAll("div").forEach((el, i)=>{
col[i] = getComputedStyle(el).backgroundColor;
el.onclick =e=> {
el.style.backgroundColor = getComputedStyle(el).backgroundColor === col[i]?'green':col[i];
};
});
</script>
</body>
</html>