Is it correct to attach the function to each element individually? Also, is my function correctly implemented?
<ul id='forShopping'>
<li><input class='ch' type='checkbox' onclick='isActive()'> Air-freshener</input></li>
<li><input class='ch' type='checkbox' onclick='isActive()'> Pampers</input></li>
<li><input class='ch' type='checkbox' onclick='isActive()'> Newspaper</input></li>
<li><input class='ch' type='checkbox' onclick='isActive()'> Toilet paper</input></li>
</ul>
function isActive () {
let elem = document.getElementsByTagName('input');
for(let i=0; i<elem.length; i++){
if(elem[i].checked == true){
elem[i].parentNode.style.backgroundColor = 'forestGreen';
}else{
elem[i].parentNode.style.backgroundColor = 'white';
}
}
}
This approach currently works, but I am uncertain about its correctness.