Reputation: 846
I am trying to calculate percentage of a number (taken from one text field) and set the calculated value to another form field. For this I have tried the below code but I am observing a mismatch in the key event/decimal places that js is detecting.
For example if I provide 100 in first input field (to calculate the percentage of 1.75), based on my js code I expect 1.75 but in the second text field it is coming as 0.175. I know this a very simple calculation but I am unable to solve this.
var reFee = document.getElementById("Estimated Referral Fee");
var LnAmt = document.getElementById("Loan Amount");
reFee.disabled = true;
LnAmt.onkeydown =
function isNumber(evt) {
evt = (evt) ? evt : window.event;
var charCode = (evt.which) ? evt.which : evt.keyCode;
if (charCode > 31 && (charCode < 48 || charCode > 57)) {
return false;
}
calcPer(LnAmt.value);
}
function calcPer(amtval) {
var pernum = 1.75;
reFee.value = (pernum / 100) * amtval;
}
<div class="form-group">
<label class="control-label col-sm-2">Loan Amount</label>
<div class="col-sm-10">
<input id="Loan Amount" placeholder="Provide Loan Amount" data-toggle="tooltip" data-original-title="Provide Loan Amount" required="required" class="form-control" name="Loan Amount" type="text" value="">
<span class="help-block"></span>
</div>
</div>
<div class="form-group">
<label class="control-label col-sm-2">Estimated Referral Fee</label>
<div class="col-sm-10">
<input id="Estimated Referral Fee" placeholder="Your referral fee" data-toggle="tooltip" data-original-title="Your referral fee" required="required" class="form-control" name="Estimated Referral Fee" type="text" value="" disabled="">
<span class="help-block"></span>
</div>
</div>
Upvotes: 4
Views: 374
Reputation: 518
var reFee = document.getElementById("Estimated Referral Fee");
var LnAmt = document.getElementById("Loan Amount");
reFee.disabled = true;
LnAmt.onkeyup =
function isNumber(evt) {
evt = (evt) ? evt : window.event;
var charCode = (evt.which) ? evt.which : evt.keyCode;
if (charCode > 31 && (charCode < 48 || charCode > 57)) {
return false;
}
calcPer(LnAmt.value);
}
function calcPer(amtval) {
var pernum = 1.75;
reFee.value = (pernum / 100) * amtval;
}
<div class="form-group">
<label class="control-label col-sm-2">Loan Amount</label>
<div class="col-sm-10">
<input id="Loan Amount" placeholder="Provide Loan Amount" data-toggle="tooltip" data-original-title="Provide Loan Amount" required="required" class="form-control" name="Loan Amount" type="text" value="">
<span class="help-block"></span>
</div>
</div>
<div class="form-group">
<label class="control-label col-sm-2">Estimated Referral Fee</label>
<div class="col-sm-10">
<input id="Estimated Referral Fee" placeholder="Your referral fee" data-toggle="tooltip" data-original-title="Your referral fee" required="required" class="form-control" name="Estimated Referral Fee" type="text" value="" disabled="">
<span class="help-block"></span>
</div>
</div>
Upvotes: 1