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 System with Vanilla JS, HTML, CSS

Paula 60

@thedoodlebakery

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


This project definitely took much more time than I expected. I had to do a lot of digging and watching videos about DOM manipulation. I used a lot of MDN to get me through this project. All in all, I feel good about the way it turned out. There was just one problem that I could not fix. When you have two buttons that have .active in their class list, I couldn't figure out a way to detect it and make sure it did not happen. If the user presses two ratings, the form will only submit the second rating as their final rating. If the user decides to click the rating again, it will deactivate due to removing the class. Is there a way to detect within in such a situation, the length and make sure that user doesn't highlight two rating? Is there a cleaner way that I could have organized my JS or CSS code?

Community feedback

@kabir-afk

Posted

Hey , I saw your code and there were some issue that I'd like to address . . .

Unnecessary JS . . .

for (li of btns){
    li.addEventListener("click", function(){
        if (this.classList.contains('inactive')){
            this.classList.remove('inactive');
        } else if (this.classList.contains('active')){
            this.classList.remove('active');
            this.classList.add('inactive');
        } else {
            this.classList.add('active');
        }
    });
}

The reason why user was able to select multiple buttons was the unnecessary JS that was working alongside your CSS. You had already declared hover,active, target states for your buttons in CSS which were then being overridden by JS . . .by removing this your btns would work properly and you won't also need to declare the .active and .inactive classes as well !

The HTML at work . . .

    <li class="rate" onclick="pickRating(value)" id="rating1" value="1">1</li>
    <li class="rate" onclick="pickRating(value)" id="rating2" value="2">2</li> 
    <li class="rate" onclick="pickRating(value)" id="rating3" value="3">3</li>
    <li class="rate" onclick="pickRating(value)" id="rating4" value="4">4</li>
    <li class="rate" onclick="pickRating(value)" id="rating5" value="5">5</li>

You can't use value attribute with something that is not an input element . . . a better pick would have been to do it something like . . .

    <li><input type="button" class="rate" onclick="pickRating(value)" id="rating1" value="1"></input></li>
    <li><input type="button" class="rate" onclick="pickRating(value)" id="rating2" value="2"></input></li> 
    <li><input type="button" class="rate" onclick="pickRating(value)" id="rating3" value="3"></input></li>
    <li><input type="button" class="rate" onclick="pickRating(value)" id="rating4" value="4"></input></li>
    <li><input type="button" class="rate" onclick="pickRating(value)" id="rating5" value="5"></input></li>

By using <input type="button"> or button , allows you to select one option at a time , you see this would have made your code a lot easier and would have also allowed you to skip the unnecessary JS I mentioned earlier...also in terms of accessibility this would have been a better approach since the user would have known that he/she is dealing with form buttons and not list items . . ., you can then later apply the required styling on buttons to make it more design accurate... hope I was able to help!! 🥂🥂

Marked as helpful

0

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