Skip to content
  • Unlock Pro
  • Log in with GitHub
Solution
Submitted over 2 years ago

Interactive rating component

Kacper•180
@kacper-reiman
A solution to the Interactive rating component challenge
View live sitePreview (opens in new tab)View codeCode (opens in new tab)

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.

Code
Select a file

Please log in to post a comment

Log in with GitHub

Community feedback

  • Adriano•42,890
    @AdrianoEscarabote
    Posted over 2 years ago

    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 deletedPosted over 2 years ago

    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
  • Thomas•50
    @thomaspaysac
    Posted over 2 years ago

    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
  • yishak abrham•2,150
    @yishak621
    Posted over 2 years ago

    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
  • Fahad Akram•140
    @FaDiiiLeo
    Posted over 2 years ago

    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•180
    @kacper-reiman
    Posted over 2 years ago

    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
Frontend Mentor logo

Stay up to datewith new challenges, featured solutions, selected articles, and our latest news

Frontend Mentor

  • Unlock Pro
  • Contact us
  • FAQs
  • Become a partner

Explore

  • Learning paths
  • Challenges
  • Solutions
  • Articles

Community

  • Discord
  • Guidelines

For companies

  • Hire developers
  • Train developers
© Frontend Mentor 2019 - 2025
  • Terms
  • Cookie Policy
  • Privacy Policy
  • License

Oops! 😬

You need to be logged in before you can do that.

Log in with GitHub

Oops! 😬

You need to be logged in before you can do that.

Log in with GitHub

How does the accessibility report work?

When a solution is submitted, we use axe-core to run an automated audit of your code.

This picks out common accessibility issues like not using semantic HTML and not having proper heading hierarchies, among others.

This automated audit is fairly surface level, so we encourage to you review the project and code in more detail with accessibility best practices in mind.

How does the CSS report work?

When a solution is submitted, we use stylelint to run an automated check on the CSS code.

We've added some of our own linting rules based on recommended best practices. These rules are prefixed with frontend-mentor/ which you'll see at the top of each issue in the report.

The report will audit all CSS, SCSS and Less files in your repository.

How does the HTML validation report work?

When a solution is submitted, we use html-validate to run an automated check on the HTML code.

The report picks out common HTML issues such as not using headings within section elements and incorrect nesting of elements, among others.

Note that the report can pick up “invalid” attributes, which some frameworks automatically add to the HTML. These attributes are crucial for how the frameworks function, although they’re technically not valid HTML. As such, some projects can show up with many HTML validation errors, which are benign and are a necessary part of the framework.

How does the JavaScript validation report work?

When a solution is submitted, we use eslint to run an automated check on the JavaScript code.

The report picks out common JavaScript issues such as not using semicolons and using var instead of let or const, among others.

The report will audit all JS and JSX files in your repository. We currently do not support Typescript or other frontend frameworks.

Oops! 😬

You need to be logged in before you can do that.

Log in with GitHub

Oops! 😬

You need to be logged in before you can do that.

Log in with GitHub

Oops! 😬

You need to be logged in before you can do that.

Log in with GitHub

Oops! 😬

You need to be logged in before you can do that.

Log in with GitHub

Oops! 😬

You need to be logged in before you can do that.

Log in with GitHub

Oops! 😬

You need to be logged in before you can do that.

Log in with GitHub