I am trying to change the label color of a single radio input. The color is changing. However, when I click on other radio buttons the class persist. I only want one radio button to have the changed class. Here is my code. So far I have tired running a loop over it and used classList Remove to take the class off of the other label elements. Everything I have tried is in the if statement, I was trying to compare the label Element to the others.
Here is a codeSandbox so its easier to answer https://codesandbox.io/s/naughty-curie-b8fd2?fontsize=14
function checkInput(e) {
let labelElement = e.target.parentNode;
let bgElement = labelElement.parentNode.parentNode;
bgElement.classList.add('checked');
if( labelElement !== labelElement ){
var oldChecked = document.getElementsByClassName('checked');
for (let i = 0; i < oldChecked.length; i++){
oldChecked[i].classList.remove('checked');
}
}
}
class ServiceLevel extends Component {
render(){
return (
<div>
<Segment>
<Menu attached="top" borderless={true}>
<Menu.Item>
<Header>Scale:</Header>
<Label className="filter_check" size="large">
<Form.Radio
label="Year"
control="input"
type="radio"
name="period"
onChange={checkInput}
/>
</Label>
<Label className="filter_check" size="large">
<Form.Radio
label="Quarter"
control="input"
type="radio"
name="period"
value="quarter"
onChange={checkInput}
/>
</Label>
<Label className="filter_check" size="large">
<Form.Radio
label="Month"
control="input"
type="radio"
name="period"
value="month"
onChange={checkInput}
/>
</Label>
<Label className="filter_check" size="large">
<Form.Field
label="Date"
control="input"
type="radio"
name="period"
value="date"
onChange={checkInput}
/>
</Label>
<Label className="filter_check" size="large">
<Form.Field
label="Interval"
control="input"
type="radio"
name="period"
value="interval"
onChange={checkInput}
/>
</Label>
</Menu.Item>
</Menu>
</Segment>
</div>
)
}
}
export default ServiceLevel;
Css is even tho you probably only need the JSX
.filter_check input {
display: none;
}
div.ui.large.label.filter_check.checked {
background-color: #00b5ad;
}