Design comparison
Solution retrospective
Hello there! I would really love to learn how to manipulate the DOM better. Is there anything in the javascript file I could have done properly? This is my first time using js in projects. Any other feedback will be appreciated.= )
Community feedback
- @NatureSon22Posted over 1 year ago
Your existing code has a repetitive structure for setting the rating when a button is clicked:
one.addEventListener("click",function(){ rating= 1; select.innerHTML = "You selected " + rating+ " out of 5."; submit.removeAttribute("disabled"); })
While this code is correct, it can lead to unnecessary repetition. As you progress in your coding journey, you will encounter iteration functions like for loops and forEach. One way to improve this code is to use the forEach method to iterate over each button element and extract its text content as the rating score:
let rating = 0; button.forEach(btn => { btn.addEventListener("click", function() { rating = parseInt(btn.textContent); select.innerHTML = "You selected " + rating + " out of 5."; submit.removeAttribute("disabled"); }); });
Marked as helpful1@nina1234567896Posted over 1 year ago@NatureSon22 Thankyou for your advice. I will definitely be using loopsin my other projects when needed =).
0
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