JSFiddle - React, Tailwind, and code Playground

HTML

<form name="f">
    <input type="checkbox" name="c" value="1">
    <input type="checkbox" name="c" value="2">
    <input type="checkbox" name="c" value="3">
    <input type="checkbox" name="c" value="4">
    <input type="checkbox" name="c" value="5">
    <input type="checkbox" name="c" value="6">
    <input type="checkbox" name="c" value="7">
    <input type="checkbox" name="c" value="8">
</form>

<input type="button" value="Conta le checkbox spuntate" id="b">

JavaScript

function conta() {
    var c = document.forms.f.elements.c, checked = 0;
    for(var i = 0, n = c.length; i < n; c.item(i++).checked && checked++);

    return checked;
}

onload = function() {
    document.getElementById("b").onclick = function() {
        alert(conta());
    };
}