0
Hello
I need to make a system that updates the total price as the items are being selected, but I don’t understand much Javascript, so I stuck to the following part: (HTML)
<input type="checkbox" name="porcoes[]" value="Tilápia" id="porcoes[]" onblur="calcular();">
<input type="number" name="porcoesQnt[]" style="width: 40px; text-align: center;" value="1" id="porcoesQnt[]" onblur="calcular();">
<label for="tilapia">Tilápia</label>
<input type="number" name="porcoesVlr[]" id="porcoesVlr[]" style="text-align: center; border: none; background-color: white;" value="1.99" onblur="calcular();" readonly><br>
<input type="checkbox" name="porcoes[]" value="Camarão empanado" id="porcoes[]" onblur="calcular();">
<input type="number" name="porcoesQnt[]" style="width: 40px; text-align: center;" value="1" id="porcoesQnt[]" onblur="calcular();">
<label for="cam">Camarão empanado</label><br>
<input type="number" name="porcoesVlr[]" id="porcoesVlr[]" style="text-align: center; border: none; background-color: white;" value="1.99" onblur="calcular();" readonly><br>
<input type="checkbox" name="porcoes[]" value="Contrafilé" id="porcoes[]" onblur="calcular();">
<input type="number" name="porcoesQnt[]" style="width: 40px; text-align: center;" value="1" id="porcoesQnt[]" onblur="calcular();">>
<label for="con">Contrafilé</label><br>
<input type="number" name="porcoesVlr[]" id="porcoesVlr[]" style="text-align: center; border: none; background-color: white;" value="1.99" onblur="calcular();" readonly><br>
[...]
<div id="resultado"> </div>
(Javascript)
function calcular() {
var i = 0;
var porcoes = document.getElementById('porcoes');
var porcoesQnt = document.getElementById('porcoesQnt');
var porcoesVlr = document.getElementById('porcoesVlr');
var total = 0;
if(porcoes != null){
for (i = 0; i < porcoes.length; i++) {
if(porcoes[i] != null){
total = total + (parseInt(porcoesVlr[i].value, 10) * parseInt(porcoesQnt[i].value, 10));
}
}
}
document.getElementById('resultado').innerHTML = total;
}
I’ve tried everything I thought but nothing solves makes it appear updated.
That’s great, but you can let me know if there’s any way to update it as you change the numbers in the quantity input?
– Eloísa C.
The code already does this, updates by checking/unchecking a checkbox or changing the quantity. test there for you to see.
– Sam
I got. Last question, besides portions I want to do this with snacks and drinks, but how do I keep this total, because each function will have different name
– Eloísa C.
Then you’d have to see how it goes.
– Sam
I do a different post? Oh, and instead of div, you can send the result to an input to pull in php?
– Eloísa C.
Have, just use . value instead of . textContent
– Sam