Build a star rating input
Build a star rating from radio inputs, so the browser handles the arrow keys, grouping and form submission, and CSS draws the stars.
A star rating is a group of radio buttons that looks different. Using real radios gives you keyboard support, form submission and screen reader labels without ARIA.
<fieldset class="rating">
<legend>Rate this post</legend>
<label><input type="radio" name="stars" value="1"> 1 star</label>
<label><input type="radio" name="stars" value="2"> 2 stars</label>
<label><input type="radio" name="stars" value="3"> 3 stars</label>
<label><input type="radio" name="stars" value="4"> 4 stars</label>
<label><input type="radio" name="stars" value="5"> 5 stars</label>
</fieldset>document.querySelector('.rating').addEventListener('change', (event) => {
console.log(`rating: ${event.target.value}`);
});rating: 4Radios with the same name form a group, so arrow keys move the selection and exactly one value is submitted. The change event bubbles to the fieldset, so one listener covers all five. Hide the circles and style the label text with CSS, and keep the visible text for screen readers rather than removing it with display: none, which would also take the input out of the tab order.
Use FormData to read the value on submit. The group semantics come from the radio input type.