Reputation: 1
Trying to get the total saving amount to display in field id saving
Here what I've so far from Stackoverflow
getPrice = function() {
var numVal1 = Number(document.getElementById("price").value);
var numVal2 = Number(document.getElementById("discount").value) / 100;
var totalValue = numVal1 - (numVal1 * numVal2)
document.getElementById("total").value = totalValue.toFixed(2);
}
<input id="price" placeholder="Amount">
<br>
<input id="discount" placeholder="Discount %">
<br>
<button onclick="getPrice()">Get total</button>
<br>
<input id="saving" placeholder="Total Savings">
<br>
<input readonly id="total" placeholder="Discounted Price">
<br>
Upvotes: 0
Views: 203
Reputation: 14844
As simple as that :
getPrice = function() {
var numVal1 = Number(document.getElementById("price").value);
var numVal2 = Number(document.getElementById("discount").value) / 100;
var totalValue = numVal1 - (numVal1 * numVal2)
var savingValue = numVal1 - totalValue;
document.getElementById("saving").value = savingValue.toFixed(2);
document.getElementById("total").value = totalValue.toFixed(2);
}
<input id="price" placeholder="Amount">
<br>
<input id="discount" placeholder="Discount %">
<br>
<button onclick="getPrice()">Get total</button>
<br>
<input id="saving" placeholder="Total Savings">
<br>
<input readonly id="total" placeholder="Discounted Price">
<br>
Upvotes: 2