Adding values in Javascript

0

In the following code, clicking the input name "goalkeeper" changes another input with the desired value. The problem is that I would like to put another field that automatically calculates the total value (goalkeeper value + technical value), but I can not. Could someone help me?

Script

<script type="text/javascript"> 
    function alterarValor(objeto, valor) {
    document.getElementById(objeto).value = valor;
    }
</script>

Html:

<input name="goleiro" onclick="alterarValor('valor_goleiro', '5');" value="5" type="radio" id="gol" />
<input name="tecnico" onclick="alterarValor('valor_tecnico', '10');" value="10" type="radio" id="tec" />



Valor do seu goleiro: <input type="text" id="valor_goleiro" value="0" disabled /><br/>
Valor do seu Técnico: <input type="text" id="valor_tecnico" value="0" disabled /><br/>


Valor Total: <input type="text" id="valor_total" value="0" disabled /><br/>
    
asked by anonymous 24.08.2016 / 22:04

1 answer

0

The solution was simple, I just created one more function, and I called both functions in onclick. Note:

<script>
function somar(){
    var gol = document.getElementById("valor_goleiro").value;
    var tec = document.getElementById("valor_tecnico").value;
    var soma = parseInt(gol) + parseInt(tec);
    document.getElementById("valor_total").value = soma;
}
</script>

And on the inputs, call the 2 onclick:

<input name="goleiro" onclick="alterarValor('valor_goleiro', '5');somar();" value="5" type="radio" id="gol" />

As I said, simpler than I expected!

    
24.08.2016 / 22:46