I have a custom Time-picker like this.
I want to change input background color when I click It and If I click another one the previous one bg should be white. But when I click second or etc previous one don't back to normal bg.
const [hours, setHours] = useState('09')
const onClickHours = (e) => {
e.preventDefault();
setHours(e.target.value)
}
const onClickFullTime = (e) => {
e.preventDefault();
setFullTime(e.target.value)
getTime(e.target.value);
changeColor(e);
}
const changeColor = (e) => {
let currentColor = e.target.attributes['data-color'].value;
let newColor = currentColor === "#fff" ? "#40a9ff" : "#fff";
e.target.style.backgroundColor = newColor;
e.target.setAttribute('data-color' , newColor);
}
const getTime= (fullTime) => {
onSelectTime(fullTime)
}
const hoursArray = [];
for (let i = 9; i < 22; i++) {
if (i < 10) {
i = '0' + i;
}
hoursArray.push(
<input key={i} onClick={onClickHours} value={i} readOnly />
)
}
const fullTimeArray = [];
for(let j = 0; j < 60; j = j + 5){
fullTimeArray.push(hours + ":" + (j< 10 ? '0' + j : j))
}
<div className="timepicker">
<div className="hours">
{hoursArray}
</div>
<div className="full-time">
{
fullTimeArray.map((time, index) => (
<input name="fullTime" data-color="#fff" key=
{index} onClick={onClickFullTime} value={time}
readOnly/>
))}
</div>
</div>