Using the JS DOM to solve the Interactive Rating Component Challenge
Design comparison
Solution retrospective
I just learnt how to work with the DOM so this challenge was perfect.
I got 90% of the challenge working. I just had a trouble removing the ".clicked" class each from previous buttons when a new rating button is clicked.
I would appreciate some help solving this.
Any additional feedback, comments or advise on best practices and code refactoring would be appreciated as well.
Community feedback
- @Cats-n-coffeePosted almost 2 years ago
Hi Nathan!
Great job for a first DOM manipulation project! I think you could solve your
clicked
class issue by using<input type="radio" >
instead of 5span
elements with class switching. The radio input would give you everything you need for this project with easy way of retrieving the selected value, and easy way to style with CSS (I'm referring to this element, not sure if you're familiar with it, there is also a styling example). I think it would also be better semantics and accessible, since this input is to make the user choose one option only. You can find ways to style these (custom) by googling it, there are a lot of posts about this. I'll give some Js feedback since you said it's your first DOM project:- your 4 variables at the top can be
const
instead oflet
since you are not re-assigning the variables. - to help with your logic as you have it now, you could keep track of the clicked rating in a global variable in your script, and use that number in your submit event handler to update the rating number, that way you don't need to update the DOM before the rating was submitted.
Once submitted you can update the rating number element and display the feedback state (so this would be in your submit handler, not in the click handler). The click event would update the rating number (in a variable) and toggle classes. Actually you might want to remove the
clicked
class on all of them, and add the one class to the selected rating after (remove all, add one). You would not be usingtoggle
in this case, butadd
andremove
class. - you can clean your code once your project is finished, remove comments, remove blank lines, ... (only blank lines in your case), it's a good habit to get into if you want to apply for jobs later!
Great job! Hope this helps, let me know if you have questions!
0@nathan-codesPosted almost 2 years ago@Cats-n-coffee thanks for literally the most helpful feedback yet! I really appreciate it. I definitely have made some notes and I'm taking on the challenge again to refactor my code based on your suggestions.
0 - your 4 variables at the top can be
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