0
I am starting in js and I was doing some studies and practicing, but I stopped here, I would like to change the color of these three Divs when we click individually on each one to yellow, but when we clicked back on it, return the standard color that was black. I tried applying some Ifs but unsuccessfully on how to condition using the standard color, could someone give a light please?
HTML
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link rel="stylesheet" href="style.css">
<title>Document</title>
</head>
<body>
<div id="jan1"></div>
<div id="jan2"></div>
<div id="jan3"></div>
<script src="main.js"></script>
</body>
</html>
CSS
*{
margin: 0;
padding: 0;
background: green;
/*display: flex;
justify-content: center;
align-items: center;*/
}
#jan1{
width: 80px;
height: 120px;
background: black;
display: inline-block;
}
#jan2{
width: 80px;
height: 120px;
background: black;
display: inline-block;
}
#jan3{
width: 80px;
height: 120px;
background: black;
display: inline-block;
}
JS
let jan1 = document.getElementById('jan1');
let jan2 = document.getElementById('jan2');
let jan3 = document.getElementById('jan3');
jan1.addEventListener('click', () =>{
jan1.style.background = 'yellow';
})
jan2.addEventListener('click', () =>{
jan2.style.background = 'yellow';
})
jan3.addEventListener('click', () =>{
jan3.style.background = 'yellow';
})
Guy creates a class type . Yellow {background: Yellow}, and when you click the element vc makes a toggleClass
– hugocsl
@Hugocsl cool, I didn’t even think of it that way, thank you
– Marcos Rocha Prado