document.getElementsByClassName
returns a NodeList
and you need to bind click event
handler for each one.
For setting checkbox border
you need to use outlineColor
and outlineStyle
properties.
For binding event
handlers you have to use a for
loop and attach a event handler for each checkbox
DOM element.
var checkboxes=document.getElementsByClassName("highlight");
for(var i=0;i<checkboxes.length;i++){
checkboxes[i].onclick=function(){
console.log("Current index: "+i);
if (checkboxes[i].checked) {
checkboxes[i].style.outlineColor = "red";
checkboxes[i].style.outlineStyle = "solid";
} else {
checkboxes[i].style.outlineColor = "none";
checkboxes[i].style.outlineStyle = "none";
}
};
}
.checkboxall {
background:#222;
padding:10px;
color:#fff;}
<div id="checkbox">
<label>
<input id="highlightall" type="checkbox"> Check all
</label>
</div>
<div class="checkboxall">
<div class="checkbox">
<label>
<input class="highlight" type="checkbox"> Checkbox1
</label>
</div>
<div class="checkbox">
<label>
<input class="highlight" type="checkbox"> Checkbox2
</label>
</div>
<div class="checkbox">
<label>
<input class="highlight" type="checkbox"> Checkbox3
</label>
</div>
</div>
But if you look, it appears errors
. That's because when page loads
, the preparePage
is invoked and it created a context
. When you click a checkbox
element, the last value saved
for i
variable will be 3
(like in the above snippet).
Why this behaviour?
Because there was only one scope within the preparePage()
function, the i
variable is only bound to a value within that scope. In other words, each time the loop changes the value of i
, it changes it for everything that references it within that scope.
After the loop finished the i
variable has value 3.
That was being said, one solution is to use let
keyword.
var checkboxes=document.getElementsByClassName("highlight");
for(let i=0;i<checkboxes.length;i++){
checkboxes[i].onclick=function(){
if (checkboxes[i].checked) {
checkboxes[i].style.outlineColor = "red";
checkboxes[i].style.outlineStyle = "solid";
} else {
checkboxes[i].style.outlineColor = "none";
checkboxes[i].style.outlineStyle = "none";
}
};
}
.checkboxall {
background:#222;
padding:10px;
color:#fff;}
<div id="checkbox">
<label>
<input id="highlightall" type="checkbox"> Check all
</label>
</div>
<div class="checkboxall">
<div class="checkbox">
<label>
<input class="highlight" type="checkbox"> Checkbox1
</label>
</div>
<div class="checkbox">
<label>
<input class="highlight" type="checkbox"> Checkbox2
</label>
</div>
<div class="checkbox">
<label>
<input class="highlight" type="checkbox"> Checkbox3
</label>
</div>
</div>
Another way is to use Immediately-invoked function expression.
For closures
, please have a look here.
var checkboxes=document.getElementsByClassName("highlight");
for(var i=0;i<checkboxes.length;i++){
(function(index){
checkboxes[index].onclick=function(){
if (checkboxes[index].checked) {
checkboxes[index].style.outlineColor = "red";
checkboxes[index].style.outlineStyle = "solid";
} else {
checkboxes[index].style.outlineColor = "none";
checkboxes[index].style.outlineStyle = "none";
}
};
}(i));
}
.checkboxall {
background:#222;
padding:10px;
color:#fff;}
<div id="checkbox">
<label>
<input id="highlightall" type="checkbox"> Check all
</label>
</div>
<div class="checkboxall">
<div class="checkbox">
<label>
<input class="highlight" type="checkbox"> Checkbox1
</label>
</div>
<div class="checkbox">
<label>
<input class="highlight" type="checkbox"> Checkbox2
</label>
</div>
<div class="checkbox">
<label>
<input class="highlight" type="checkbox"> Checkbox3
</label>
</div>
</div>