Design comparison
Solution retrospective
Hey, community this is my solution for the interactive rating component challenge. I have a few questions:-
- How to pop up a warning when the rating value is not selected?
- How to change the background color of the selected rating when another rating is selected It would be helpful if you could answer my questions. Other feedback is also appreciated.
Community feedback
- @warrenleePosted over 2 years ago
Hey Satyam, looking at your code, in your
submitbtn
event listener forclick
I would check ifselectedrating
is defined/not empty before deciding to show the thank you message.For your second question in your function
ratingdisplay
you would typically do a reset before firing the selected state, and I would use classes and it'll make things easier and concise.What you have now is this.
function ratingdisplay(event) { ratingbutton.forEach(() => { event.target.style.backgroundColor = "hsl(25, 97%, 53%)"; event.target.style.color = "white"; selectedrating = event.target.value; }); }
Firstly the
forEach
loop is redundant as it's apply the same styles to the same element, so instead I would use thisforEach
loop to reset the styles to what they were before and then apply the active state after.function ratingdisplay(event) { ratingbutton.forEach(() => { // reset your styles for each ratingbutton here }); // apply selected state here event.target.style.backgroundColor = "hsl(25, 97%, 53%)"; event.target.style.color = "white"; selectedrating = event.target.value; }
But if you use classes instead.
function ratingdisplay(event) { ratingbutton.forEach((el) => { // reset your styles for each ratingbutton here el.classList.remove('selected'); }); // apply selected state here event.target.classList.add('selected'); selectedrating = event.target.value; }
Or even better still do comparison checks and use
toggle
instead.function ratingdisplay(event) { ratingbutton.forEach((el) => { // Use toggle to evaluate if the class should be added or removed el.classList.toggle('selected', el.value === event.target.value); }); selectedrating = event.target.value; }
Hope this helps
Marked as helpful1@satyammjhaPosted over 2 years ago@warrenlee Thanks for such a detailed explanation. I have made the changes and it worked great.
1
Please log in to post a comment
Log in with GitHubJoin our Discord community
Join thousands of Frontend Mentor community members taking the challenges, sharing resources, helping each other, and chatting about all things front-end!
Join our Discord