I have two buttons I want to toggle class to the currently clicked button. The condition is user only select one button at a time and the user can also deselect both buttons. Here is my code stackblitz example. I have toggle class but now I am facing problem to deselect selected button. Please help.
HTML
<button *ngFor="let button of buttons" class="btn rounded m-4" [ngClass]="(selectedButton == button) ? 'btn-primary' : 'btn-default'" (click)="onClickButton(button)">
<i [class]="button.class"></i>
</button>
TS
onClickButton(button): void {
if (this.selectedButton === button) {
this.selectedButton = null;
} else {
this.selectedButton = button;
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With