Good night.
You can use jQuery to facilitate the handling of some HTML elements.
With all due respect, I took the liberty of tweaking a few things in your code. Please see if the example below helps you in any way.
Explanation: For each field where the user type the value, I created two functions in jQuery, one for the keyUp event (After he finishes pressing some key) and another is the change function (if the user clicks the up or down arrow, which is inside the field).
The idea is that every time some value is typed in any of the three fields, the script is taking the value of all the fields and multiplying by the fixed value that is defined in the VAR tags. Finally, you are summing all the results of the multiplications and displaying the total value in the tag valorTotal
To help you, if you need to take the full purchase amount, there is a field input type=Hidden with the ID and name value equals Pay, if you send this value to another page that will process the payment
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<title>Orçamento</title>
</head>
<body>
<div>
<h1 class="titulo">Orçamento</h1>
</div>
<div>
Quantidade item 1 =
<input type="number" name="campoUm" id="campoUm">
x R$ <var id="valorUm">1500</var> = <var id="resultado1"></var>
</div>
<div>
Quantidade item 2 =
<input type="number" name="campoDois" id="campoDois">
x R$ <var id="valorDois">250</var> = <var id="resultado2"></var>
</div>
<div>
Quantidade item 3 =
<input type="number" name="campoTres" id="campoTres">
x R$ <var id="valorTres">500</var> = <var id="resultado3"></var>
</div>
<div>
Total = <var id="valorTotal"></var>
<input type="hidden" name="valorTotalCompra" id="valorTotalCompra" value="" />
</div>
<script type="text/javascript">
$(document).ready(function(){
//exemplo utilizando jQuery
$(document).on('keyup','#campoUm',function(){
let total1 = $('#'+this.id+'').val() * $('#valorUm').text();
let total2 = $('#campoDois').val() * $('#valorDois').text();
let total3 = $('#campoTres').val() * $('#valorTres').text();
let valorTotal = total1 + total2 + total3;
$('#resultado1').empty();
$('#resultado1').html('R$ '+total1);
$('#valorTotal').empty();
$('#valorTotal').html('R$ '+valorTotal);
$('#valorTotalCompra').val('');
$('#valorTotalCompra').val(valorTotal);
});
$(document).on('change','#campoUm',function(){
let total1 = $('#'+this.id+'').val() * $('#valorUm').text();
let total2 = $('#campoDois').val() * $('#valorDois').text();
let total3 = $('#campoTres').val() * $('#valorTres').text();
let valorTotal = total1 + total2 + total3;
$('#resultado1').empty();
$('#resultado1').html('R$ '+total1);
$('#valorTotal').empty();
$('#valorTotal').html('R$ '+valorTotal);
$('#valorTotalCompra').val('');
$('#valorTotalCompra').val(valorTotal);
});
$(document).on('keyup','#campoDois',function(){
let total2 = $('#'+this.id+'').val() * $('#valorDois').text();
let total1 = $('#campoUm').val() * $('#valorUm').text();
let total3 = $('#campoTres').val() * $('#valorTres').text();
let valorTotal = total1 + total2 + total3;
$('#resultado2').empty();
$('#resultado2').html('R$ '+total2);
$('#valorTotal').empty();
$('#valorTotal').html('R$ '+valorTotal);
$('#valorTotalCompra').val('');
$('#valorTotalCompra').val(valorTotal);
});
$(document).on('change','#campoDois',function(){
let total2 = $('#'+this.id+'').val() * $('#valorDois').text();
let total1 = $('#campoUm').val() * $('#valorUm').text();
let total3 = $('#campoTres').val() * $('#valorTres').text();
let valorTotal = total1 + total2 + total3;
$('#resultado2').empty();
$('#resultado2').html('R$ '+total2);
$('#valorTotal').empty();
$('#valorTotal').html('R$ '+valorTotal);
$('#valorTotalCompra').val('');
$('#valorTotalCompra').val(valorTotal);
});
$(document).on('keyup','#campoTres',function(){
let total3 = $('#'+this.id+'').val() * $('#valorTres').text();
let total1 = $('#campoUm').val() * $('#valorUm').text();
let total2 = $('#campoDois').val() * $('#valorDois').text();
let valorTotal = total1 + total2 + total3;
$('#resultado3').empty();
$('#resultado3').html('R$ '+total2);
$('#valorTotal').empty();
$('#valorTotal').html('R$ '+valorTotal);
$('#valorTotalCompra').val('');
$('#valorTotalCompra').val(valorTotal);
});
$(document).on('change','#campoTres',function(){
let total3 = $('#'+this.id+'').val() * $('#valorTres').text();
let total1 = $('#campoUm').val() * $('#valorUm').text();
let total2 = $('#campoDois').val() * $('#valorDois').text();
let valorTotal = total1 + total2 + total3;
$('#resultado3').empty();
$('#resultado3').html('R$ '+total2);
$('#valorTotal').empty();
$('#valorTotal').html('R$ '+valorTotal);
$('#valorTotalCompra').val('');
$('#valorTotalCompra').val(valorTotal);
});
});
</script>
</body>
</html>
You can put the HTML of these fields?
– Sam
It’s not looking good, but you can see visually better what I’m trying to do.. But from there, I don’t know how to save variables to do the calculations. I would use the id/class/name value for this?
– eloypellegrino