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

Kacper 180

@kacper-reiman

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


I couldn't figure out how to prevent user from submitting without having rating selected, but I didn't know how to check if rating button is not clicked and display an alert message. Tried also to check if :focus on any button is active and if not, then display an alert, but I couldn't reach it in JS. My next idea was to change buttons into radio inputs, hide them and catch their labels as buttons, but I think a label can't have an active state like :focus so I gave up. Any ideas how to solve the problem? Also any other feedback and advice would be highly appreciated.

Community feedback

Adriano 34,090

@AdrianoEscarabote

Posted

Hello Kacper, how are you? I truly loved your project's outcome, however I have some advice that I hope you'll find useful:

To make the submit button work only when the user selects a number we can do this:

ratingButton.forEach((rate) => {
  rate.addEventListener("click", () => {
    choosenRating.innerHTML = rate.innerHTML;
    submitButton.addEventListener("click", () => {
      thanksSection.classList.remove("hidden");
      ratingSection.classList.add("hidden");
    });
  });
});

The remainder is excellent.

I hope it's useful. 👍

Marked as helpful

0

yishak abrham 2,150

@yishak621

Posted

@AdrianoEscarabote maybe thats possible but instead of nesting the event listeners and replacing the innerHTML we can simply use forEach method to iterate over the rating btns Array(converting the nodelist to Array by spread operators ...)and using if statments to check if there is active rating btns ...

Marked as helpful

1
yishak abrham 2,150

@yishak621

Posted

@AdrianoEscarabote and also for the rating btns we r not gonna replacing the innerHTML to make it active we can just adding the active class

function callBack() { 
   //logic-when we click on the btns container[ratingbtns] 
   ratingBtns.addEventListener('click', (e) => { 
     const id = e.target.dataset.id; 
     console.log(id); 
     if (id) { 
       //remove active state from btns 
       eachBtns.forEach((btn) => { 
         btn.classList.remove('btn-active'); 
         e.target.classList.add('btn-active'); 
         textResult.innerHTML = `You selected <span class="result-number"> ${id}</span> out of 5`; //printing the selected value to result 
       }); 
     } 
   }); 
 }

Marked as helpful

1

@VCarames

Posted

Hey there! 👋 Here are some suggestions to help improve your code:

Why did you comment out the input radio code? That is the correct way of creating this challenge.

  • The “icons/images” in this component serve no other purpose then to be decorative; They add no value. There alt tag should be left blank and have an aria-hidden=“true” to hide them from assistive technology.

More Info:📚

https://www.w3.org/WAI/tutorials/images/

If you have any questions or need further clarification, feel free to reach out to me.

**Happy Coding!**🎄🎁

Marked as helpful

0
Thomas 50

@thomaspaysac

Posted

Hi Kacper,

The simplest solution I found was to add the "disabled" attribute to the submit button, make it unclickable using pointer-events: none; in CSS while disabled, and then make the JavaScript remove the disabled attribute using a simple for loop so that when any rating is chosen the disabled attribute is removed :

  userRatingChoice[i].addEventListener('click', function() {
    SUBMIT_BUTTON.removeAttribute('disabled');;
  })
}

Marked as helpful

0
yishak abrham 2,150

@yishak621

Posted

I use these code to check whether the user clicks or not

//Submit btn click event 
 submitBtn.addEventListener('click', () => { 
   callBack(); 
   const newArray = [...eachBtns]; //spread operator to convert them to array 
   newArray.forEach((btn) => { 
     //checking if the user click on rating btn-at least one of it makes it true 
     if (btn.classList.contains('btn-active')) { 
       ratingCard.classList.add('hidden'); 
       selectedCard.classList.remove('hidden'); 
       home.classList.remove('hidden');

Marked as helpful

0

@FaDiiiLeo

Posted

Hey Kacper, you have done a good work!

You can use following tips regarding your question about preventing user from submitting if a rating is not selected:

Instead of using buttons for ratings, you can use <input type="button" > and use value attribute to show rating numbers and also to store selected rating in a variable in javascript. You will need to add click event listeners on all rating buttons and store their value in variable using e.target.value. You can initialize the variable with 0 and when user submits the rating, do a simple if else and check if variable is equal to 0 (means no rating is selected) show an alert message, else you can show your thank you card with the selected rating. Hope this helps and if you have any question or need further help you can ask here.

Marked as helpful

0
Kacper 180

@kacper-reiman

Posted

thanks for all your effort to help me. i've made some improvements in JS, feel free to check them out. also added animations to make switching windows look a bit nicer. But i've noticed another problem : I checked if everything works properly in chrome and mozilla devtools touch mode for mobile devices, and it did. However, tapping button on actual mobile device(iPhone 11) doesn't give it properties defined by :focus state, instead it leaves button with its :hover properties.

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