function calculateTotal() {
document.getElementById("Total1").value = document.getElementById("Unit1").value * document.getElementById("quantity1").value
}
Asked
Active
Viewed 26 times
-1
2 Answers
0
First make sure the values of inputs are numbers by parse Float() Then you can use the toFixed() method in JavaScript to format a number with two decimals. The toFixed() method formats a number with a specific number of digits to the right of the decimal.

Mahmoud Hassan
- 328
- 1
- 8
0
The elements don't have a .value property, instead you can use .textContent
to get and change the value stored.
If you are working with numbers only, you can use .toFixed() to round the product.
const quantityEl = document.getElementById("quantity1");
const unitEl = document.getElementById("unit1");
// .toFixed() takes a parameter for number of digits to appear after decimal, in this case 2
const val = (quantityEl.textContent * unitEl.textContent).toFixed(2);
document.getElementById("value").textContent = val;

suravshrestha
- 329
- 1
- 5
- 14