0
I’m a beginner in jQuery and I’m not being able to add up the values of two different inputs and show in a third input the result without refreshing the screen. Could someone help me?
These are the form fields:
<div class="form-group" id="formvalor"> <!-- Campo para somar -->
<label class="control-label col-md-3 col-sm-3 col-xs-12">Valor <small>(R$) </small><span class="required">*</span></label>
<div class="col-md-3 col-sm-3 col-xs-12" id="valor_total">
<input type="text" class="form-control col-md-7 col-xs-12 decimal som" name="valor" id="valor" value="<?php echo $vvalor; ?>" readonly><br>
</div>
</div>
<div class="form-group"> <!-- Campo para somar -->
<label class="control-label col-md-3 col-sm-3 col-xs-12">Entrada <small>(R$) </small><span class="required">*</span></label>
<div class="col-md-3 col-sm-3 col-xs-12" id="entrada">
<input type="text" class="form-control col-md-7 col-xs-12 decimal som" name="entrada" value="<?php echo $vvalor; ?>"><br>
</div>
</div>
<div class="form-group"> <!-- Campo para mostrar resultado -->
<label class="control-label col-md-3 col-sm-3 col-xs-12">Saldo <small>(R$) </small><span class="required">*</span></label>
<div class="col-md-3 col-sm-3 col-xs-12" id="saldo_total">
<input type="text" class="form-control col-md-7 col-xs-12 decimal" name="saldo" value="<?php echo $vsaldo; ?>" readonly>
</div>
</div>
I’m trying to use this script:
$("#valor_total, #entrada").on('blur', function(){
var valortotal = $('#valor_total').val();
var entrada = $('#entrada').val();
var resultado = parseInt(valortotal) + parseInt(entrada);
$('#saldo_total').val(resultado);
});
When all fields are then filled in, the sum result does not appear. A div=saldo_total
goes blank.
Could someone help me?
Where are you calling the script? Is it at the click of a button? At the on-change event? At the page load? https://api.jquery.com/category/events/ If you want something dynamic, try "change"
– Celso Marigo Jr
It is in the exchange of input. With onblur. But I believe that change would also solve
– Gabriel
Possible duplicate of Add inputs with jquery and real time
– Mateus Veloso
Hello @Matthew Veloso. I tried to follow the comments of this topic but could not solve my problem with it, so I opened this
– Gabriel
Check below my answer.
– Mateus Veloso