How on pure js compare the digital value of the element
and if it is greater than 0, then give this element (or another element) an extra class?
How on pure js compare the digital value of the element
and if it is greater than 0, then give this element (or another element) an extra class?
The usual comparison operator is used for this: if(условие){если да, то выполнить это}else{если нет, то выполнить это};
function check() { var num = document.querySelectorAll('input[type="checkbox"]:checked').length; if(num > 0){ document.getElementById('checkbox').classList.add("s1"); document.getElementById('checkbox').classList.remove("s0"); }else{ document.getElementById('checkbox').classList.add("s0"); document.getElementById('checkbox').classList.remove("s1"); }; }; #checkbox {width:20px;height: 20px;} #checkbox.s0 {background-color: black} #checkbox.s1 {background-color: green} <body onload="check()"> <input type="checkbox" onclick="check()"> <input type="checkbox" onclick="check()"> <input type="checkbox" onclick="check()"> <div id="checkbox"> </div> Source: https://ru.stackoverflow.com/questions/612531/
All Articles