Submitted about 1 year ago
Interactive Rating Compenent with HTML, CSS and Javascript
@adrianmartinsen
Design comparison
SolutionDesign
Solution retrospective
Toyed with the idea of converting node list of buttons to array. Could not see that there was any major upside to this other than being able to use methods like map. For a single component Javascript's forEach seemed to be fine.
Also, if someone has a good idea how to clean up the nested loop that would be cool.
Community feedback
- @K01wfdPosted about 1 year ago
Hi Adrian, in your project as there will be only one active button at the time you can modify the forEach loop like this:
ratingButtons.forEach((btn) => { btn.addEventListener("click", (e) => { selectedRating = btn.textContent; let activeButton = document.querySelector(".active-button"); if(activeButton) { activeButton.classList.remove("active-button"); } btn.classList.add("active-button"); }); });
good luck.
Marked as helpful0
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