My problem is that, I have button like below
<button type="button" id="btn_all">All</button>
<button type="button" id="btn_A">A</button>
<button type="button" id="btn_B">B</button>
<button type="button" id="btn_C">C</button>
And my javascript is
let btn_all = document.getElementById("btn_all");
let btn_A = document.getElementById("btn_A");
let btn_B = document.getElementById("btn_B");
let btn_C = document.getElementById("btn_C");
let allButtons = [btn_all,btn_A,btn_B,btn_C];
let abt = btn_A.style.backgroundColor = "red";
function changeColor (e) {
let currentColor = e.target.style.backgroundColor;
if (currentColor != "red") {
e.target.style.backgroundColor = "red";
}else{
e.target.style.backgroundColor = "";
}
}
function changeColorAll (e) {
let currentColor = e.target.style.backgroundColor;
if (currentColor != "red") {
allButtons.forEach( function(element, index) {
element.style.backgroundColor = "red";
});
}
else {
allButtons.forEach( function(element, index) {
if (index === (allButtons.length -3)) {
element.style.backgroundColor = "red";
}else{
element.style.backgroundColor = "";
}
});
}
}
btn_all.addEventListener("click", changeColorAll);
btn_A.addEventListener("click", changeColor);
btn_B.addEventListener("click", changeColor);
btn_C.addEventListener("click", changeColor);
1 - When I click on button A,B,C, to change it's background to red, button All also change it's backgroudColor to red.
2- When I click on All button to change its and others to no backgroudColor, at least one button still red and prevent it not changeable the color. Anybody can help please.
UPDATE
I want to update my problems.
1 - When I first load the page all four button has no backgroudcolor except button A has background in red. If I click on B, C, button, I want button All change to red also.
2- When I click on button All to change all button to no background color, I want one of these buttons( A, B, C) still red and can not click to change color.