Checkbox with function

1

Good afternoon, I'm building a form with checkboxes that call a function through onclick

<body> 
    <form name="questao1" method="post" onsubmit="sendToDB();"> 
        <br><input type="checkbox" name="Q1[]" value="Enfermagem" onclick="getPontos(1)"> Garantir a saúde das pessoas 
        <br><input type="checkbox" name="Q1[]" value="Eletroeletronica" onclick="getPontos(2)"> Máquinas e instalações elétricas.
        <br><br><input type="submit" value="Próximo" onclick="finalizar()">     
    </form>
</body>

To perform this function:

    function getPontos(pontos){

                efmg = 0;
                eltelt = 0;
                info = 0;
                alm = 0;
                pltc = 0;
                log = 0;
                adm = 0;
                qmc = 0;
                ma = 0;

                if (pontos == 1){ efmg = efmg + 1; }   
                if (pontos == 2){ eltelt = eltelt +1; }

}

function finalizar(){   
           if(efmg>eltelt){alert('Enfermagem');}else{alert('Eletroeletronica');} 
        }

However, every time I call this function the values return to zero, and I need the value to hold for the next checkbox click. the problem is that if I do not assign the initial values to zero, the function does not work, and the end does not show any result. Can someone help me solve it? Thanks in advance :)

    
asked by anonymous 03.12.2016 / 20:19

1 answer

1

Use global variables, out of their function, that initialize to 0 when the page loads, and each time the function is called, they change the value of those variables.

var efmg = 0;
var eltelt = 0;
var info = 0;
var alm = 0;
var pltc = 0;
var log = 0;
var adm = 0;
var qmc = 0;
var ma = 0;

function getPontos(pontos){

            if (pontos == 1){ efmg = efmg + 1; }   
            if (pontos == 2){ eltelt = eltelt +1; }

}

function finalizar(){   
       if(efmg>eltelt {alert('Enfermagem');}else{alert('Eletroeletronica');} 
    }

Something that occurred to me when I was seeing too, you are not initializing the variables inside the function (var xxx, var yyy), so I guess you do that elsewhere in the code, you can initialize it as 0 right there. >     

03.12.2016 / 20:33