Interactive rating component

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.
Please log in to post a comment
Log in with GitHubCommunity feedback
- @AdrianoEscarabote
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 - Account deleted
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 anaria-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 - The “icons/images” in this component serve no other purpose then to be decorative; They add no value. There
- @thomaspaysac
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 - @yishak621
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 - @FaDiiiLeo
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 - @kacper-reiman
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.
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