class MyComponent {
public allButtons = [false, false, false];
public btn1 = document.getElementById('btn1');
public btn2 = document.getElementById('btn2');
public btn3 = document.getElementById('btn3');
public myMap: Map<number, HTMLDivElement> = new Map<number, HTMLDivElement>();
constructor() {
// hardcoded index mapped to the btn1
this.myMap.set(0, btn1);
this.myMap.set(1, btn2);
this.myMap.set(2, btn3);
}
// the function that is called for the main 3 buttons, pass in an index
onBtnClick(index: number) {
// reset all buttons array
this.resetAllButtons();
// assign true to the selected index
this.allButtons[index] = true;
// loop though all buttons
// - add active to the index that is true
// - remove active class from the indindex add/remove active class off the button
this.allButtons.forEach( (isBtnActive, i) => {
const btn = this.myMap.get(i);
if(btn) {
isBtnActive ? btn.classList.add('active') : btn.classList.remove('active');
}
});
}
// every click will always reset all values to false
resetAllButtons() {
this.allButtons = [false, false, false];
}
}
const comp = new MyComponent();
Please Whitelist JSFiddle in your content blocker.
Help keep JSFiddle free for always by one of two ways:
Whitelist JSFiddle in your content blocker (two clicks)
Go PRO and get access to additional PRO features →
Join the 4+ million users, and keep the JSFiddle dream alive.
Ad-free
All ads in the editor and listing pages are turned completely off.
Use pre-released features
You get to try and use features (like the Palette Color Generator) months before everyone else.
Fiddle collections
Sort and categorize your Fiddles into multiple collections.
Private collections and fiddles
You can make as many Private Fiddles, and Private Collections as you wish!
Console
Debug your Fiddle with a minimal built-in JavaScript console.