mercredi 5 août 2020

Update variable when a certain radio is selected

I have a number of radio btns with ids all numbered 1-15. When they are selected, I want 1 to be added to one of 3 variables (countA countB or countC) depending on which number they have for their id. I can't work out what's wrong with my code!!

var countA = 0
            var countB = 0
            var countC = 0 
            function check() {
                var selectors = document.getElementsByTagName("input")
                var numSelectors = selectors.length
                for (var i = 0; i < numSelectors; 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++
                            }
                            showCounts()
                    }
                            }
                            }

            function showCounts() {
                document.getElementById("testp").innerHTML = "Count A: " + countA + "Count B: " + countB + "Count C: " + countC
            } 

Aucun commentaire:

Enregistrer un commentaire