Subtract a percentage from a value

hello programmers i have a simple script does only addition but i want it to able to minus a pecentage from a value

eg 100 - 1.5 %

answer is 98.5

i hope u guys understand what am asking, thank you.

<form>
<input name="numeric" type="text" size="8" maxlength="4" />
<input name="numeric" type="text" size="8" maxlength="4" />
<input name="total" type="text" size="8" readonly="readonly" />
<input type="button" value="Calculate" onclick="calculate(this.form);" />
</form>

<script type="text/javascript">
function calculate(form){
var sum = 0;
for(var i = 0; i < form.numeric.length; i++)sum += Number(form.numeric[i].value);
form.total.value = sum;
}
</script>

That’s the answer. Just multiply your number by 0.985

yh i know thats the answer, the script only does addition

ok i got something but i want to make the calculation to show on keypress not on button click

<script>
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);
}
</script>




<form >
  <label>price</label>
  <input id="price" type="price" data-validetta="required,price" required="">
  <label>discount</label>
  <input id="discount" type="text" required="">  
  <button type="button" onclick="getPrice()"> Get total </button> 
    <input id="total" type="text" required="">  

</form>

onkeypress might be what you’re looking for.

ok i just tried it, but the calculation seems to get shorten

like with on onkeypress instead of 98.5 i get 9.85

If you get 9.85 you’ve done your math wrong. Maybe show us the offending line?

ok i change the event from onkeypress to onchange its working now

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.