I've implemented a star rating widget in an HTML form within my React JS application. The widget I am using can be found here. When a user clicks on one of the five stars, the user's information is saved to the server.
Currently, the selected stars remain highlighted even after the response comes back from the server. I would like to reset the stars to their unselected state for the user's next action. However, I'm unsure how to achieve this using TypeScript/Javascript. I've attempted to modify the code as follows:
let ratingElement = document.querySelectorAll("#ratingField > label");
if (ratingElement.length > 0) {
for (let i = 0; i < ratingElement.length; i++) {
ratingElement[i].style.color = "#ddd";
}
}
The above code successfully resets the color, but there are other CSS properties like hover
, checked
, and unchecked
that need to be reset as well. How can I include these properties to reset the stars to their initial stage?
Below is the CSS for the widget:
.rating {
border: none;
float: left;
}
/* Additional CSS properties here */
.rating > input:checked + label:hover, etc..
And here is the relevant HTML code:
<fieldset
className="rating"
style={{ marginLeft: 5, marginRight: 5 }}
id="ratingField"
>
<input type="radio" id="star5" name="rating" value="5" />
<label onClick={() => this.shortlist(5)} className="full" htmlFor="star5" title="Awesome" />
/* HTML for other stars here */
</fieldset>
If anyone has a solution or suggestions on how to accomplish this, it would be greatly appreciated! Thank you.