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

Interactive rating component using HTML, CSS, JS.

accessibility, bem, emotion, foundation, lighthouse
Petru Banceanu•110
@Petru14
A solution to the Interactive rating component challenge
View live sitePreview (opens in new tab)View codeCode (opens in new tab)

Solution retrospective


The most difficult part was writing JS, I struggled for a few hour's trying to figured out how to make the pop-up results to be exactly the same number with the one that was clicked. I'm still unsure with that part, as I tried to figure how to change the background color only for the selected number, but I couldn't find any helpful solution, so I will appreciate any helpful tips from u guys regarding that part of the code, and any kind of feedback as well. Thanks!

Code
Loading...

Please log in to post a comment

Log in with GitHub

Community feedback

  • Elaine•11,360
    @elaineleung
    Posted almost 3 years ago

    Hi Petru, I did this challenge a while ago and recently updated it with a different approach using radio buttons, but I think my old code might still help you out a bit. First, in your buttons, you'll want to add a value for each button, like this:

    <button class="btn" value="1" />1</button>
    

    Here's roughly what the JS looked like:

    const cardEl = document.getElementById("cardEl");  
    const scoreBtns = document.querySelectorAll(".btn");
    
    let selected = null;  // this will keep track of which button is pressed
    
    scoreBtns.forEach((btn) => {
      btn.addEventListener("click", (event) => {
        event.preventDefault();
        selected = btn.value;
        selectScore(selected);
      });
    });
    
    // this function handles what happens when the button is pressed
    
    function selectScore(selected) {
      scoreBtns.forEach((btn) => {
    
      // first, remove all classes
        btn.classList.remove("is-selected");
    
      // checks button to see if value matches selected
        if (btn.getAttribute("value") === selected) {
    
      // adds class to button
          btn.classList.add("is-selected");
        }
      });
    }
    

    In the CSS, you just make sure you write a rule for the button with the selected class and also the button hovered:

    .btn:hover {
      background-color: // btn color here;
    }
    .is-selected {
      background-color: // selected btn background color here;
    }
    

    Hope this helps you out a bit! If you're interested in my radio button accessibility version (which is more complicated with labels and inputs being hidden and tabbed), you can check out my solution here: https://www.frontendmentor.io/solutions/responsive-interactive-rating-component-SeBo-aR4gB

    Marked as helpful
  • Pradeep Saini•990
    @pradeeps4ini
    Posted almost 3 years ago

    Hi, @petru14. How are you?

    You have done a great job implementing the design.

    I would like to suggest some changes, if you don't mind.

    1. You've wrote the HTML for the card in a <header>. You should've used <main> instead. <header> is used for the heading of the page. For the main content, which is card in our design, should be in the <main> element. For the footer, you can use <footer> element. Using right elements for the right content is better for readability and accessibility. You can read more here... https://developer.mozilla.org/en-US/docs/Glossary/semantics.

    2. You don't need "d-flex and container" styling on the, "container", "cont", and "content" wrappers. They have block level elements, they automatically position themselves vertically.

    In the "cont" and "cont-1" wrapper, you don't need a max-width: 600px and width: 100%. Because you've specified a "max-width:380px" in the "content" wrapper element. With these changes you won't need media queries to resize the "content" wrapper element. It will auto resize itself.

    In the "cont" and "cont-1" add a property of "margin-inline: 1rem;" to give some side margin.

    1. You can set the property "flex-wrap: wrap;" on <ul>. This way the <li> items will break to 2nd line when the size is too small for them to fit.

    2. I wrote a simple demo code to show, how you can get the chosen rating and display it. Read it, modify it and learn from it. https://codepen.io/pradeeps4ini/pen/OJvZGev?editors=1011

    https://codepen.io/pradeeps4ini/pen/qBojyLK If you want to create something that can refresh the page with a button without refreshing the browser tab. I hope, i could help you.:)

    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
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