vendredi 31 juillet 2020

Add to a variable each time a radio is checked?

Using only Javascript, I am trying to get 1 to be added to one of my variables (countA countB or countC) each time a radio btn is checked depending of what id a radio btn has. For some reason, my code will not run. What have I done wrong? HTML:

    <input type = "radio" class="a" name="radio" id="1"> 1 <br>
            <input type="radio" class="b" name="radio" id="2"> 2<br>
            <input type="radio" class="c" name="radio" id="3"> 3 <br>
        </div><br>       
            <input type="radio" class="a" name="radio" id="4"> 4<br>
            <input type="radio" class = "b" name="radio" id="5"> 5<br>
            <input type="radio" class="c" name="radio" id="6"> 6<br>
        </div><br>       
            <input type="radio" class="a" name="radio" id="7"> 7 <br>
            <input type="radio" class="b" name="radio" id="8"> 8 <br>
            <input type="radio" class="c" name="radio" id="9"> okay. 9<br>
        </div><br>
            <input type="radio" class="a" name="radio" id="10"> 10<br>
            <input type="radio" class="b" name="radio" id="11"> 11<br>
            <input type="radio" class="c" name="radio" id="12"> 12<br>
        </div><br>       
            <input type="radio" class="a" name="radio" id="13">13<br>
            <input type="radio" class="b" name="radio" id="14"> 14<br>
            <input type="radio" class="c" name="radio" id="15"> 15 <br>
        </div><br>
<p id="testp"></p>

JAVASCRIPT:

 var countA = 0
            var countB = 0
            var countC = 0 
            
            function check() {
                var radios = document.getElementsByTagName("input").length
                var selector = document.getElementsByTagName("input")
                for (var i = 0; i < radios; i++) {
                    if (selector[i].checked) {
                        if (selector[i].getAttribute("id") == 1 || selector[i].getAttribute("id") == 4 || selector[i].getAttribute("id") == 7 || selector[i].getAttribute("id") == 10 ||
                        selector[i].getAttribute("id") == 13) {
                            countA++
                        }
                        if (selector[i].getAttribute("id") == 2 || (selector[i].getAttribute("id") == 5 selector[i].getAttribute("id") == 8 || selector[i].getAttribute("id") == 11 ||
                        selector[i].getAttribute("id") == 14) {
                            countB++
                        }
                        if (selector[i].getAttribute("id") == 3 || selector[i].getAttribute("id") == 6 || selector[i].getAttribute("id") == 9 || selector[i].getAttribute("id") == 12 ||
                        selector[i].getAttribute("id") == 15) {
                            countC++
                            }
                
                                            
                                                }
                document.getElementById("testp").innerHTML = "Count A: " + countA + "Count B: " + countB + "Count C: " + countC
                            }
}

Aucun commentaire:

Enregistrer un commentaire