<div id="a">A</div>
<div id="b">B</div>
<div id="c">C</div>
<style>
#a, #b, #c {
height: 100px;
width: 100px;
background-color:red;
font-size: 100px;
margin-bottom: 20px;
}
#a:active, #b:active, #c:active {
background-color: blue;
}
</style>
When I click on #a, only #a changes color, when I click on #b, only #b changes color, and when I click on #c, only #c changes color.
What can I do so that when I click #a OR #b OR #c, all of them change color at the same time?
I do not know if the solution requires JS, but if it does, please answer using vanilla JS
2
Answers
You would definitely need JavaScript (or jQuery) to accomplish this. Here is a simple way to do it with event listeners.
The code below will apply a class to all elements when one of them is clicked and then remove it when the click is released.
You need a simple script that applies to each element.
And here’s how to do it…
.active
class and design it accordingly.The .forEach() method will loop through all the elements in the array and will perform the given action for all.
I hope that helps!