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

214
Views
Change color of all clicked buttons

I have a set of buttons in an html page of the following form:

<button
      id="testID"
      mat-mini-fab
      ngClass="list-button"
      (click)="onClick($event)"
    >
      Press
    </button>

I try to change the color of each button belonging to .list-button class after clicking on it using the following css code:

.list-button:focus {
  background-color: #7d698d;
}

However, while the color of the button I click each time changes (the color of all the previously clicked buttons also changes back to their original color). How could I fix it? I want all the clicked buttons to remain their new color.

I also tried assigning an id to the buttons of this class and changing their color inside the onClick() method as follows without success. The same problem remains. Could you help me, please?

  onclick(event: any) {
    const btn = document.getElementById('testID');
    if (btn) btn.style.backgroundColor = '#7d698d';
  }
about 4 years ago · Juan Pablo Isaza
3 answers
Answer question

0

No need for js. Use the focus selector in your stylesheet.

The :focus selector is allowed on elements that accept keyboard events or other user inputs.

~ W3

button.list-button:focus {
  background-color: #7d698d;
  color: white;
}
<button class="list-button">Click</button>

Edit ~ with js. This method uses .querySelectorAll("button") and toggles the styled class .focus each time the button(s) are clicked. This works well when having multiple buttons.

// Get all the elements on the basis of query selector (button)
let btn = document.querySelectorAll("button");

for (var i = 0; i < btn.length; i++) {
  (function(index) {
    btn[index].addEventListener("click", function() {
      console.log("Clicked Button: " + index);

      let isPresent = false;

      // Checks if the class is present or not
      this.classList.forEach(function(e, i) {
        if (e == "focus") {
          isPresent = true;
        } else {
          isPresent = false;
        }
      });

      // Toggles the presence of class (.focus) on the basis of the isPresent variable
      if (isPresent) {
        this.classList.remove("focus");
      } else {
        this.classList.add("focus");
      }
    });
  })(i);
}
.focus {
  background-color: #7d698d;
  color: white;
  cursor: pointer;
}
<button id="btn">Click</button>
<button id="btn">Click</button>
<button id="btn">Click</button>

about 4 years ago · Juan Pablo Isaza Report

0

You need to get the id of the button that fired the event

onclick(event: any) {
  const btn = document.getElementById(event.target.id);
  if (btn) btn.style.backgroundColor = '#7d698d';
}
about 4 years ago · Juan Pablo Isaza Report

0

The reason why the color of the previously clicked buttons is resetting after you click on another one is that the focus is set on the last clicked button. Try using the code in the Venkatesh's answer.

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!