Design comparison
Solution retrospective
Hi Frontend Community,
I went into this project looking to use 2 event listeners, one on the input buttons in the fieldset element and the other on the submit button.
I struggled a bit before I realized I needed to apply a class name on the buttons before looping over them. Originally, I was trying to gain access to them through the CSS color change that occurred when the user presses the button but that was a dead end for me.
Is there any other way I could have gotten to the data other than the way I coded it?
//Event listener to add class of "selected" to the button the user clicks:
document.querySelectorAll("#fieldset input[type=button]").forEach(button => {
button.addEventListener("click", function() {
document.querySelectorAll("#fieldset input[type=button]").forEach(button => {
//By calling the remove first, we ensure that only one button has the "selected" class at any given time.
button.classList.remove("selected");
});
//Only the clicked button will have the "selected" class.
button.classList.add("selected");
});
});
Community feedback
- @VCaramesPosted over 1 year ago
Hey @Stacy-Riley 👋! Here is some feedback to further improve your code:
Regarding your question, all that can be achieved using
css
with theactive
pseudo class. There is no need ⚠️ to useJavaScript
for that. All JS should be doing is obtaining the rating value and hiding/displaying the old/new content.More info: 📚
To get the rating value, there is no need ⚠️ to loop through all the buttons instead use the following
const rating = form.selected-score.value;
.If you have any questions or need further clarification, you can always check out my submission and/or feel free to reach out to me.
Happy Coding! 👾
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