I have these four divs with same class-name and want to change color everytime when I click on them but the trick is that if I click on first div its color will change and when I click to next div the previous color should comes to its default color and the one I clicked should change.
<div class="box">Box1</div>
<div class="box">Box2</div>
<div class="box">Box3</div>
<div class="box">Box4</div>
Like this…..NA
3
Answers
What you wrote in the description of the problem isn’t really the same as what you wrote in the question title, but I think I should answer what’s described, to achieve this functionality using JavaScript, here’s an example code snippet that demonstrates how to change the color of the clicked box while reverting the color of the previously clicked box back to its default color;
You can simply use the JavaScript methods to achieve this even without using the loops.
Check the code below..