PDA

View Full Version : Javascript that calculates % of another calculation



padders01
06-11-2015, 12:31 PM
I have code which calculates 8% of the number entered, so for example is in the code below. If you enter "1" in the first box" and "100" in the second box the Total Refund (including 8%) = 108

JavaScript code:


$(document).ready(function() {
var sum = 0;
function calcSum(prevVal) {
var val1 = $('#val1').val();
var val2 = $('#val2').val();
var val3 = $('#val3').val();
var val4 = $('#val4').val();
this.sum = parseFloat(val1) * parseFloat(val2) + parseFloat(val3) * parseFloat(val4);
return this.sum;
}
var subAmt = $("#sub"),
taxAmt = $("#tax"),
totAmt = $("#total");
$(".val").each(function() {
var prevVal = this.value / 1,
self = this;
$(this).keyup(function() {
subAmt.val(calcSum.call(self, prevVal));
totAmt.val(this.sum + this.sum * parseFloat(taxAmt.val() / 100));
prevVal = self.value;
});
});
});


http://jsfiddle.net/2k1zr59u/3/

What I would like to do is to calculate 20% of the 8% in the second to the bottom box. So in this example the answer should be 1.60.

Then I would like to show in the total box 106.40 (108 - 1.60).

padders01
06-12-2015, 08:07 AM
Can anyone help me please?

padders01
06-12-2015, 10:55 AM
I received some help and have it working now, thanks