Hi.
I’m trying to write a JS script that checks for checked checkboxes and adds a class “on” if checkbox is checked:
<body>
<form action="index.html" method="post">
<input type="checkbox" name="ve" value="ve1"><label for="ve">Vechicle1</label><br />
<input type="checkbox" name="ve" value="ve2"><label for="ve">Vechicle2</label><br />
<input type="checkbox" name="ve" value="ve3"><label for="ve">Vechicle3</label><br />
<input type="checkbox" name="ve" value="ve4"><label for="ve">Vechicle4</label><br />
<button type="button" name="button" onclick="checkOn()" value="Run function"></button>
</form>
<script type="text/javascript">
function checkOn() {
let boxes = document.forms[0];
for (let i = 0; i < boxes.length; i++) {
if (boxes[i].checked){
boxes[i].className = "on";
}
}
}
</script>
</body>
Im using onclick event on button to run my funcion, but how to run my function without adding events to html?