Not Found
Not Found
Not Found
Not Found
Not Found
Not Found
Not Found
Not Found
Not Found
Not Found
Not Found
Not Found
Not Found
Not Found
Not Found
Not Found
Not Found
Not Found
Not Found
Not Found

Submitted

responsive rating component

@nina1234567896

Desktop design screenshot for the Interactive rating component coding challenge

This is a solution for...

  • HTML
  • CSS
  • JS
1newbie
View challenge

Design comparison


SolutionDesign

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

Nature Son 1,100

@NatureSon22

Posted

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 helpful

1

@nina1234567896

Posted

@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 GitHub
Discord logo

Join 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