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

Interactive Rating Component

Satyam Jha 400

@satyammjha

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


Hey, community this is my solution for the interactive rating component challenge. I have a few questions:-

  1. How to pop up a warning when the rating value is not selected?
  2. How to change the background color of the selected rating when another rating is selected It would be helpful if you could answer my questions. Other feedback is also appreciated.

Community feedback

Warren 630

@warrenlee

Posted

Hey Satyam, looking at your code, in your submitbtn event listener for click I would check if selectedrating is defined/not empty before deciding to show the thank you message.

For your second question in your function ratingdisplay you would typically do a reset before firing the selected state, and I would use classes and it'll make things easier and concise.

What you have now is this.

function ratingdisplay(event) {
  ratingbutton.forEach(() => {
    event.target.style.backgroundColor = "hsl(25, 97%, 53%)";
    event.target.style.color = "white";
    selectedrating = event.target.value;
  });
}

Firstly the forEach loop is redundant as it's apply the same styles to the same element, so instead I would use this forEach loop to reset the styles to what they were before and then apply the active state after.

function ratingdisplay(event) {
  ratingbutton.forEach(() => {
    // reset your styles for each ratingbutton here
  });

  // apply selected state here
  event.target.style.backgroundColor = "hsl(25, 97%, 53%)";
  event.target.style.color = "white";
  selectedrating = event.target.value;
}

But if you use classes instead.

function ratingdisplay(event) {
  ratingbutton.forEach((el) => {
    // reset your styles for each ratingbutton here
    el.classList.remove('selected');
  });

  // apply selected state here
  event.target.classList.add('selected');
  selectedrating = event.target.value;
}

Or even better still do comparison checks and use toggle instead.

function ratingdisplay(event) {
  ratingbutton.forEach((el) => {
    // Use toggle to evaluate if the class should be added or removed
    el.classList.toggle('selected', el.value === event.target.value);
  });

  selectedrating = event.target.value;
}

Hope this helps

Marked as helpful

1

Satyam Jha 400

@satyammjha

Posted

@warrenlee Thanks for such a detailed explanation. I have made the changes and it worked great.

1

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