css - Javascript: Change label background color on checkbox -


i trying change background color of label in each checkbox in form based on checked/unchecked state of checkbox.so far got change initially, won't change when uncheck:

http://jsfiddle.net/7wncl/4/

javascript:

function statecheck(layer) { var mylayer = document.getelementbyid(layer);  if(mylayer.checked = true){  mylayer.style.backgroundcolor = "#bff0a1";  } else {  mylayer.style.backgroundcolor = "#eee";  }; } 

html:

<form action="" method="get"> <label title="alabama" id="alabama"><input type="checkbox" value="checkbox" onchange="statecheck('alabama')" />al</label> <label title="alaska" id="alaska"><input type="checkbox" value="checkbox" onchange="statecheck('alaska')" />ak</label> <label title="american samoa" id="americansamoa"><input type="checkbox" value="checkbox" onchange="statecheck('americansamoa')" />as</label> </form> 

css:

label { margin:0px 2px 4px 2px;  padding: 1px; background-color: #eee; display: block; width: 50px; } 

http://jsfiddle.net/7wncl/20/

mylayer.checked = true 

is assignment, not condition.

if (mylayer.checked = true) 

is every time evaluated as

if (true) 

and else part never executed. change to:

if (mylayer.checked === true) 

also, should check input, , not layer, doesn't have checked property:

if (mylayer.childnodes[0].checked === true) 

Comments

Popular posts from this blog

python - How to create a legend for 3D bar in matplotlib? -

java - Multi-Label Document Classification -

php - Dynamic url re-writing using htaccess -