Hello, I hope you can help me, I have the following script and only the result3 marks me NaN
<label class="col-sm-2 control-label">Importe Asegurado</label>
<div class="col-sm-2"><input id="valor" type="number" onkeyUp="calcular();" name="importe" class="form-control m-b"></div>
<label class="col-sm-2 control-label">Cuota Base (%)</label>
<div class="col-sm-2"><input id="valor2" type="text" onkeyUp="calcular();" name="cuota" class="form-control m-b"></div>
<span id="result" type="text" name="prima"></span><br>
<span id="resultado3" type="text" name="iva"></span><br>
<span id="resulta2" type="text" name="total"></span>
<script>
function calcular(){
//Obtienes el valor
var valor = document.getElementById("valor").value;
var valor2 = document.getElementById("valor2").value;
var result= document.getElementById('result');
var total= document.getElementById('total');
//le descuentas el 8% y lo agregas al HTML
var descuento = parseInt(valor)*valor2/100;
var iva = parseInt(valor)*valor2*1.16/100;
//agrega los resultados al DOM
result.innerHTML = 'Prima: ' + descuento.toFixed(2);
resulta2.innerHTML = 'Total: ' + iva.toFixed(2);
resultado3.innerHTML = 'iva: ' + iva - descuento;
}
</script>
But if I remove result3 'iva: ' + if it gives me the result but without a name and with more decimals
Here it is better:
What I did was modify this:
So that it stays like this:
You were calculating the VAT minus the discount within the innerHTML, this should not be done, it is better to do it in a variable and print it directly, regards :)
I recommend that you do the calculation in another variable. This makes it easier to display the correct result. It would look like this:
You just have to correct the following line of your script
by
Using parentheses I hope it helps you