Skip to content
Advertisement

How to change color of div that’s selected and others be same?

I have a dynamic array of divs representing numbered lists i.e.

JavaScript

I have 2 classes, dot which is a gray color, active which is red. How can change the color of div to red which is clicked/selected and the rest be gray?

Advertisement

Answer

You use React, right? You could write a Component instead of your div that should contain a state variable of isClicked and a function handleClick. HandleClick can then set isClicked to true. Then you can have conditional styling, like:

JavaScript
User contributions licensed under: CC BY-SA
7 People found this is helpful
Advertisement