milla-Mill
milla-Mill

Reputation: 1

Show total savings in discount calculate

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

Answers (1)

johannchopin
johannchopin

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

Related Questions