[removed] Change label background color on checkbox

后端 未结 7 1450
执笔经年
执笔经年 2021-01-25 06:37

I am trying to change the background color of a label in each checkbox in a form based on the checked/unchecked state of the checkbox.So far I got it to change initially, but it

7条回答
  •  盖世英雄少女心
    2021-01-25 06:58

    There are couple of mistakes, in your script.

    • You are passing lable id and checking for labelId.checked which does not exist
    • You are using = in if condition which should be ==

    This is how you JS method should look like

    function statecheck(layer, checkbox) {
        var myLayer = document.getElementById(layer);
        //myLayer.style.backgroundColor = "#bff0a1";
        if(checkbox.checked == true){
            myLayer.style.backgroundColor = "#bff0a1";
            } else {
            myLayer.style.backgroundColor = "#eee";
        };
    
    }
    

    HTML

    
    

提交回复
热议问题