I created simple Javascript that calculates total based on two input variables (price, quantity). The script should take specified values in HTML form and show the calculated result in the “Total” field. The calculations should be updated on oninput event. How to accomplish this correctly, to pass values of input fields (price, quantity) to Javascript on oninput event and dynamically update result in “Total” field?
<!DOCTYPE html>
<html>
<body>
<form>
<label for="price">Price:</label><br>
<input type="number" id="price" name="price" value="0.0458" min="0" step="0.0001"><br><br>
<label for="quantity">Quantity:</label><br>
<input type="number" id="quantity" name="quantity" value="1" min="1"><br><br>
<label for="total">Total:</label><br>
<input type="text" id="total" name="total" value="" readonly><br><br>
<input type="reset" value="Reset">
</form>
<script>
let B1 = 0.0458;
let B2 = 1;
let B3 = B2 * 10.702 * 1.02;
let B4 = B3 * B1;
let B5 = B3 * 0.00321;
let B6 = Math.round((B4 + B5) * 100) / 100;
let B7 = B3 * 0.0121219;
let B8 = 4.62;
let B9 = B7 + B8;
let B10 = B3 * 0.0038;
let B11 = B6 + B9 + B10;
let B12 = Math.floor((B11 * 1.21) * Math.pow(10, 2)) / Math.pow(10, 2);
element.addEventListener("input", setPrice);
function setPrice() {
document.getElementById("price").value = B1;
}
element.addEventListener("input", setQuantity);
function setQuantity() {
document.getElementById("quantity").value = B2;
}
</script>
</body>
</html>
