1

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.

4
  • Could you provide your codes on an online editor like Jsfiddle/codepen so that we can check the output? Commented Jan 9, 2021 at 3:45
  • Here is my code in jsfiddle.net/47g6fmzd Commented Jan 9, 2021 at 4:02
  • Could you clarify what the expected behaviour for each button click is? Commented Jan 9, 2021 at 11:46
  • I want to update my problems. It's may because my English is not good enough to explain to you of my problem. 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. If you have anything for me to clarify please tell me. I really need this code. Commented Jan 10, 2021 at 9:26

2 Answers 2

2

For your 1st point, add a condition in the changeColor method to check whether all the buttons are red or not.

if (btn_A.style.backgroundColor == "red" && btn_B.style.backgroundColor == "red" && btn_C.style.backgroundColor == "red") {
    btn_all.style.backgroundColor = "red";
} else {
    btn_all.style.backgroundColor = "";
}

For your 2nd point, remove the if-else condition from the changeColorAll else part and just update the color directly.

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) {
            element.style.backgroundColor = "";
            btn_A.style.backgroundColor = "red";
    });
}}

Also, add a condition in the changeColor to check whether all the buttons are not of red color, if not then change the color of a button

if (btn_A.style.backgroundColor == "" && btn_B.style.backgroundColor == "" && btn_C.style.backgroundColor == "") {
    btn_A.style.backgroundColor = "red"
}
Sign up to request clarification or add additional context in comments.

Comments

0
//HTML
$('#Button').prop('disabled', true);
//JS
document.getElementById("Button").disabled = !0;

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.