Given the following unordered HTML/JSX list:
<ul className="settings">
<li>
<input type="radio" name="settings" id="beginner" ref={selectedSetting} onClick={handleSettingsChange}/>
<label htmlFor="beginner">Beginner</label>
</li>
<li>
<input type="radio" name="settings" id="intermediate" ref={selectedSetting} onClick={handleSettingsChange}/>
<label htmlFor="intermediate">Intermediate</label>
</li>
<li>
<input type="radio" name="settings" id="expert" ref={selectedSetting} onClick={handleSettingsChange}/>
<label htmlFor="expert">Expert</label>
</li>
<li>
<input type="radio" name="settings" id="custom" ref={selectedSetting} onClick={handleSettingsChange}/>
<label htmlFor="custom">Custom</label>
</li>
</ul>
with a submit button that fires the following function:
const onSubmit = (e: MouseEvent): void => {
console.log(selectedSetting.current)
}
I want to validate that at least one of these radio buttons have been selected. As you can see I was using a single useRef variable here which would cause the "custom" radio button to be logged every time. The straight forward solution, then, is to have a useRef for each radio button but is there an easier way to do this? Can I instead give the ul tag a ref and do something similar to jQuery to see if one of them has been selected?