Skip to content
  • Unlock Pro
  • Log in with GitHub
Solution
Submitted about 3 years ago

interactive rating component main using HTML, CSS and Vanilla Js

Jesus Rebeitte•110
@Rebeitte
A solution to the Interactive rating component challenge
View live sitePreview (opens in new tab)View codeCode (opens in new tab)

Solution retrospective


For this solution I used html, css and vanilla JS. I used a for loop to iterate over the botoms to select the ranks, also I added a buttom to close the submit page.

I'm open to any advice, I had problems with the rating point, because I wanted to print the option that user selected and not only "You selected 4 out of 5".

Code
Couldn’t fetch repository

Please log in to post a comment

Log in with GitHub

Community feedback

  • Tesla_Ambassador•3,070
    @tesla-ambassador
    Posted about 3 years ago

    Hey Rebeitte! Congratulations on completing this challenge! It looks really sharp and I hope you had fun completing it. 👏 Here's a few pointers:

    • To answer your question, you might want to add an id tag to the <span> that contains the number 4 in your html. Then you can select that span in your javaScript using document.querySelector( '#element' ) or whatever selector you are comfortable with. So you need to save the value of the button selected by the user in a variable and set that variable as the innerText property of the <span> in your javaScript. To know a little more on innerText, follow this link

    Happy coding and keep up the good work 👍

    Marked as helpful
  • Elaine•11,360
    @elaineleung
    Posted about 3 years ago

    Hey Rebeitte, I also had a look at your code and the component, and I found that while the selected number does change the background color, it doesn't change back when another number is selected, which means that if I take turns clicking on all the 5 buttons, all of them would end up gray, which would not be like the design. Ideally, only the most recently clicked one would remain gray. I see that you're using toggle in your JS; what that means is that, the class would get added or removed depending on whether the button is clicked, but it doesn't get added/removed if other buttons are clicked.

    To solve this problem, you'd have to add another iterator in your loop; I've added a forEach() function here for simplicity, which you'll see below. Just to follow Tesla_Ambassador's suggestion above, I've added a line here to show how you can show the score; you can take the number directly from the clicked items element using textContent and then just add that to the textContent of the <span> tag.

    First, add an id in your HTML within the <span> tag:

    <p class="rank-select">
      You selected <span id="score">4</span> out of 5
    </p>
    

    Then try this in your JS:

    const scoreEl = document.getElementById('score');
    
    for (let i = 0; i < ranks.length; i++) {
      ranks[i].addEventListener('click', function selecting() {
    
        // this would remove the style class from all the items first, kind of like a reset
        ranks.forEach((rank) => rank.classList.remove('number_selected')); 
    
        ranks[i].classList.add('number_selected');
    
        ranks[i].style.color = 'white';
    
        // This would add the number to the span tag
        scoreEl.textContent = ranks[i].textContent
      });
    }
    
    

    Hope this helps!

    Marked as helpful

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

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