JS - Multiply and Show in text

1

Hello,

I'd like to create a code that does a multiplication of what the Client inserts into the Input text field.

This multiplication would be ...

Valor de taxa = 0.8;
Valor do Cliente = "O valor inserido"

Valor do Cliente * Valor de taxa.

I would like it to appear in text ... How do I do it?

JAVASCRIPT

function calculate() {

var coins = document.getElementById('number').value;
var rate = 0.8;
var total = coins * rate;

}
    
asked by anonymous 20.05.2018 / 17:05

2 answers

1

You can stay "watching" your input and whenever there was any change in it, it would trigger the function with the desired logic!

$( document ).ready(function() {
    var input1 = $("#valor_unitario");
    var result = $("#result");
    
    $(input1).on("change keyup paste", function(){
      var resultado = $(input1).val() * 0.8;
      result.html(resultado);
    })
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script><labelfor="valor_unitario">Vlr. Unit.</label>
<input type="text" name="valor_unitario" id="valor_unitario" style="text-align: center" required>
<p>Resultado: <span id="result"></span></p>
    
20.05.2018 / 17:33
1

Through this example Jquery you can tailor your needs:

<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script><scriptsrc="https://cdnjs.cloudflare.com/ajax/libs/jquery.mask/1.14.15/jquery.mask.min.js"></script>

<input type="text" id="valor_do_cliente">
<span id="resultado">0</span>

<script type="text/javascript">
    var taxa = 0.8;
    $('#valor_do_cliente').keyup(function(){
        var valor = $('#valor_do_cliente').val();
        var resultado = Math.ceil(valor * taxa);
        $('#resultado').html(resultado);
    });
</script>
    
20.05.2018 / 17:18