I am implementing simple functionality of arithimatic operators using Javascript in my HTML form. But it is not working. It seems right, but dont know why it is not showing value in desired field. Please have a little look into my following code and guide.
var input = $('[name="ServiceTotalCost"],[name="GrantRequest"],[name="MatchingShare"]'),
ServiceTotalCost = $('[name="ServiceTotalCost"]'),
GrantRequest = $('[name="GrantRequest"]'),
MatchingShare = $('[name="MatchingShare"]');
input.change(function() {
var ServiceTotalCost = (isNaN(parseInt(ServiceTotalCost.val()))) ? 0 : parseInt(ServiceTotalCost.val());
var GrantRequest = (isNaN(parseInt(GrantRequest.val()))) ? 0 : parseInt(GrantRequest.val());
if (ServiceTotalCost > GrantRequest) {
Difference = ServiceTotalCost - GrantRequest;
MatchingShare.val(Difference);
} else if (GrantRequest >= ServiceTotalCost) {
PercentAge = (GrantRequest * 20) / 100;
MatchingShare.val(PercentAge);
}
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class="col-md-12">
<div class="form-group form-float">
<h2>*Support Requried Through Grant</h2><br>
<div class="form-line">
<input type="number" class="form-control" name="ServiceTotalCost" id="ServiceTotalCost" required>
<label class="form-label">Total cost of the service (Rs.)</label>
</div>
</div>
</div>
<div class="col-md-12">
<div class="form-group form-float">
<div class="form-line">
<input type="number" class="form-control" name="GrantRequest" max="500000" id="GrantRequest" required>
<label class="form-label">Amount of grant requested (Rs.)*</label>
</div>
</div>
</div>
<div class="col-md-12">
<div class="form-group form-float">
<div class="form-line">
<input type="number" name="MatchingShare" id="MatchingShare" class="form-control" readonly>
<label class="form-label">Matching share by the applicant (Rs.)</label>
</div>
</div>
</div>