0

I have used a jsFiddle that I found on another question on here that will work perfectly if I can get the subtotal to multiply instead of addition.

I am sure this is a really easy fix, but I'm struggling to make the change.

Can someone help me please?

Thank you in advance

http://jsfiddle.net/77uxK/48/

$(document).ready(function(){
    var sum = 0;
    function calcSum(prevVal){
        sum = sum - prevVal + (this.value/1);
        return 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(sum + sum*parseFloat(taxAmt.val()/100));
            prevVal = self.value;
        });
    });
});
4
  • which is the line where you want to multiply Commented Jul 9, 2013 at 8:30
  • Ah yeah sorry, the subtotal line, the total line should then calucate the tax Commented Jul 9, 2013 at 8:33
  • 4
    'Java' is to 'JavaScript' as 'Car' is to 'Carpet'. If you had so much as glanced at the description of the tags you were slapping on this question, that difference might have become apparent.. Commented Jul 9, 2013 at 8:33
  • 3
    Andrew, I originally did post it as Javascipt only, but I received an error saying I didn't have high enough reputation. Please do accepet my apologises Commented Jul 9, 2013 at 8:36

1 Answer 1

3

just change the function to

function calcSum(prevVal) {
    var val1 = $('#val1').val();
    var val2 = $('#val2').val();
    sum = parseInt(val1) * parseInt(val2); // parseFloat based on your need change it
    return sum;
}

jsfiddle link

Sign up to request clarification or add additional context in comments.

7 Comments

Hi Soul, I copied this code into the jsFiddle and it didn't seem to work
just enter values in the value1 and value2 and click on the jsfiddle link given above @padders01
do you know how to make the total only show 2 decimal places?
@padders01 the output is it nor the subtotal user .toFixed(2)
Ok I will try that. I have noticed if I put decimals into the value boxes then it calculates wrongly. For example try 2.5 in both boxes. It returns a sub total of 6.25
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.