I want to calculate the total prices of products using the input text values.
The html code and javascript snippet are as follows:
$('input').keyup(function(){ // run anytime the value changes
var firstValue = parseFloat($('#value1').val()) || 0; // get value of field
var secondValue = parseFloat($('#value2').val()) || 0; // convert it to a float
var subtotal1 = firstValue * 10;
var subtotal2 = secondValue * 15;
$('#subtotal1').html(subtotal1); // output it
$('#subtotal2').html(subtotal2); // output it
$('#finaltotal').html(subtotal1 + subtotal2); // output it
});
<!-- Product 1 -->
<div>
<label>Product 1</label>
<div><input type="text" id="value1"><?php echo ' x 10USD = ';?><span id="subtotal1"></span>USD</div>
<!-- Product 2 -->
<div>
<label>Product 2</label>
<div><input type="text" id="value2"><?php echo ' x 15USD = ';?><span id="subtotal2"></span>USD</div>
<!-- Total -->
<div>
<label>Total:</label>
<div><span id="#finaltotal"></span>USD</div>
</div>
The final result should be something like the following: [1 ] x 10USD = 10USD [2 ] x 15USD = 30USD Total: 40USD
However, the above code does not work properly. Is there a good way to get it done?
Thanks for your help.