Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

217
Views
How do I create an active class with css and javascript for my5 star rating react component?

I would like to be able to have a rating component that consist of five stars like this where the active class is added to the span element when clicked and all the span elements before it. (like a. 5 star rating would just have to be clicked once on the 5th span) The active class would need to be removed from any spans after it when clicked as well. How can I achieve this?

<span>*</span>
<span>*</span>
<span>*</span>
<span>*</span>
<span>*</span>
</div>

// React is loaded and is available as React and ReactDOM (I'm not using imports)

  render() {
    return null;
  }
}

document.body.innerHTML = "<div id='root'> </div>";
  
const rootElement = document.getElementById("root");
ReactDOM.render(<Rating />, rootElement);

document.querySelectorAll("span")[2].click();
console.log(document.getElementById("rating").outerHTML);
about 4 years ago · Santiago Gelvez
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!