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

196
Views
Using a Normal Button (Non-Radio/Checkbox), how can I ensure only one value is chosen AND deselects another if one is chosen?

This is a very cut-down version to visualize what I'd like to do.

My HTML

 <p class="call">Please select a number.</p>
  <div class="button-flex">
   <button class="rating">1</button>
   <button class="rating">2</button>
   <button class="rating">3</button>
   <button class="rating">4</button>
   <button class="rating">5</button>
  </div>

My JS

let allBtns = document.querySelectorAll(".rating");

allBtns.forEach((option) => {
  option.addEventListener("click", function () {
    option.style.backgroundColor = "orange";
  });
});

Essentially, I want a user to be able to click on only a single rating (1 thru 5) that will eventually be used to update a string.

The main problem I'd like to tackle right now is being able to select one value, pick another one, and have the previously chosen value be deselected. I was able to find a method using radios, and checkboxes will work, but I also want to keep the button style because that's how it's supposed to be displayed.

Is this possible with a normal button element?

about 4 years ago · Juan Pablo Isaza
2 answers
Answer question

0

let allBtns = document.querySelectorAll(".rating");

allBtns.forEach((option) => {
  option.addEventListener("click", function (event) {
    allBtns.forEach(el => {
      el.classList.remove('active');
    });
    
    event.currentTarget.classList.add('active');
  });
});
.rating.active {
  background-color: orange;
}
<p class="call">Please select a number.</p>
<div class="button-flex">
 <button role="radio" class="rating">1</button>
 <button role="radio" class="rating">2</button>
 <button role="radio" class="rating">3</button>
 <button role="radio" class="rating">4</button>
 <button role="radio" class="rating">5</button>
</div>

Identifies the currently selected button element with .active

When the button is selected, remove the .active from all Button Elements and insert the .active into the newly selected button.

about 4 years ago · Juan Pablo Isaza Report

0

You should memoize last button, then change it back on next click, like this:

let allBtns = document.querySelectorAll(".rating");
let lastBtn;

allBtns.forEach((option) => {
  option.addEventListener("click", function () {
    lastBtn && (lastBtn.style.backgroundColor = null);
    (lastBtn = option).style.backgroundColor = "orange";
  });
});
<p class="call">Please select a number.</p>
  <div class="button-flex">
   <button class="rating">1</button>
   <button class="rating">2</button>
   <button class="rating">3</button>
   <button class="rating">4</button>
   <button class="rating">5</button>
  </div>

about 4 years ago · Juan Pablo Isaza Report
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!