In an HTML form, I have the following section, which I want to act like radio buttons but appear like regular buttons.
<span>Minimum Skill Level:</span><br />
<input type='button' id='skMin1' class='btn-sm' name='skMin' value='1' checked>
<input type='button' id='skMin2' class='btn-sm' name='skMin' value='2'>
<input type='button' id='skMin3' class='btn-sm' name='skMin' value='3'>
<input type='button' id='skMin4' class='btn-sm' name='skMin' value='4'>
<input type='button' id='skMin5' class='btn-sm' name='skMin' value='5'>
By making the name the same for all of the buttons in the group, they act like radio buttons in that clicking on a second deselects the first.
I have a section of JavaScript which adds a listener to each button and saves the value. It changes the background color of the button to show that it's been selected.
let radioButton1 = document.querySelectorAll("[name=skMin]").forEach(function(e) {
e.addEventListener('click', () => {
e.style.background = 'cyan';
drillSession.skillMin = e.value;
})
})
It looks like I want, and I'm getting the data just fine, but there's one big problem. Clicking on a second button within the group does change the value, but I can't figure out how to unhighlight the previously selected option, so the user won't be confused by seeing multiple highlighted buttons. Any suggestions?