0
I have the code below which multiplies the value of the field Valor with the field value Qtd and the result automatically appears in the field Total value. Look at:
function calcular() {
var valor1 = parseInt(document.getElementById('valor').value, 10);
var valor2 = parseInt(document.getElementById('qtd').value, 10);
var multiplicar = valor1 * valor2;
if (multiplicar == 'NaN') {
document.getElementById('valorTotal').value = 0;
} else {
document.getElementById('valorTotal').value = multiplicar;
}
}
<div class="col-md-4">
<div class="form-group label-floating">
<label class="control-label">Valor:</label>
<input type="text" name="Valor" id="valor" class="form-control" onfocus="calcular()">
</div>
</div>
<div class="col-md-4">
<div class="form-group label-floating">
<label class="control-label">Qtd:</label>
<input type="text" name="Qtd" id="qtd" class="form-control" onblur="calcular()">
</div>
</div>
<div class="col-md-12">
<div class="form-group">
<div class="form-group label-floating">
<label class="control-label">Valor Total:</label>
<input type="text" name="ValorTotal" id="valorTotal" class="form-control">
</div>
</div>
</div>
The problem is that in the field valorTotal was appearing Nan Before typing in the second field and to prevent, I created the conditional in the code above, but now I can’t type in the field. How can I fix this?
"only now I can’t type in the field", it doesn’t seem to be happening in your code.
– Woss
Nan is happening because you calculate the value the moment the first input gains focus. At this point there is no value typed. You can use the keydown.
– DaviAragao