Calculate Price in Javascript [duplicate]

1

I have a table where Quantity x Price in javascript will be calculated. Following the rule of decimal places.

I'm currently using the basic function, but it works only if I put whole numbers or "." separating (15.50). By separating with a comma, the calculation no longer works.

function calcular() {
    var cpqtde = $('#cpqtde').text();
    var cpvalor = $('#cpvalor').val();
    
    $('#cpliqu').text(cpqtde * cpvalor);
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script><b>Quantidade:</b><spanid="cpqtde">10</span><br>
<b>Valor:</b> <input id="cpvalor" onblur="calcular()"  type="text"><br>

<b>Resultado:</b>
<div id="cpliqu"></div>
    
asked by anonymous 20.06.2018 / 20:32

1 answer

4

Change type from input to number which will start working with , or . and will also make it impossible for the user to enter invalid characters. I hope I understood the problem and helped.

function calcular() {
    var cpqtde = $('#cpqtde').text();
    var cpvalor = $('#cpvalor').val();
    
    $('#cpliqu').text(cpqtde * cpvalor);
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script><b>Quantidade:</b><spanid="cpqtde">10</span><br>
<b>Valor:</b> <input id="cpvalor" onblur="calcular()"  type="number"><br>

<b>Resultado:</b>
<div id="cpliqu"></div>
    
20.06.2018 / 21:01