0

I have a table in which I want to subtract values in columns. However, instead of subtracting the values it keeps adding up.

jsFiddle.

HTML:

<table>
    <tr class="parent-ca">
        <td>
             <h4><em>Contract Amount</em></h4>

        </td>
        <td>
            <input type="text" class="rr" /> //all rr should be subtracted
        </td>
        <td>
            <input type="text" class="rr1" /> //all rr1 should be subtracted
        </td>
        <td>
            <input type="text" class="rr2" />
        </td>
        <td>
            <input type="text" class="rr3" />
        </td>
    </tr>
    <tr class="parent-abc">
        <td>
             <h5><em>Contract Amount</em></h5>

        </td>
        <td>
            <input type="text" class="rr" />
        </td>
        <td>
            <input type="text" class="rr1" />
        </td>
        <td>
            <input type="text" class="rr2" />
        </td>
        <td>
            <input type="text" class="rr3" />
        </td>
    </tr>
    <tr class="parent-a">
        <td>
             <h5><em>Contract Amount</em></h5>

        </td>
        <td>
            <input type="text" class="rr" />
        </td>
        <td>
            <input type="text" class="rr1" />
        </td>
        <td>
            <input type="text" class="rr2" />
        </td>
        <td>
            <input type="text" class="rr3" />
        </td>
    </tr>
    <tr class="parent-b">
        <td>
             <h5><em>Contract Amount</em></h5>

        </td>
        <td>
            <input type="text" class="rr" />
        </td>
        <td>
            <input type="text" class="rr1" />
        </td>
        <td>
            <input type="text" class="rr2" />
        </td>
        <td>
            <input type="text" class="rr3" />
        </td>
    </tr>
    <tr class="parent-c">
        <td>
             <h5><em>Contract Amount</em></h5>

        </td>
        <td>
            <input type="text" class="rr" />
        </td>
        <td>
            <input type="text" class="rr1" />
        </td>
        <td>
            <input type="text" class="rr2" />
        </td>
        <td>
            <input type="text" class="rr3" />
        </td>
    </tr>
    <tr class="nettotal tt table-sub" data-minus="ca,contract,other2">
        <td>
             <h4>Net Cash</h4>

        </td>
        <td>
            <input type="text" id="rr" />
        </td>
        <td>
            <input type="text" id="rr1" />
        </td>
        <td>
            <input type="text" id="rr2" />
        </td>
        <td>
            <input type="text" id="rr3" />
        </td>
    </tr>
</table>

jQuery:

function calcSubMTotal(p) {
    var minus = 0;
    var minus_total = $('#' + p + '');


    $('.' + p + '').each(function (i) {
        if (!isNaN(this.value) && this.value.length !== 0) {
            minus -= this.value; //trying to subtract but this just adds up rather than subtract
        }
    });

    minus_total.val(minus);


}

$('input[type=text]').on('keyup', function () {
    calcSubMTotal("rr");
    calcSubMTotal("rr1");
    calcSubMTotal("rr2");
    calcSubMTotal("rr3");
});
input
  • 7,503
  • 25
  • 93
  • 150
  • 2
    Your code is actually doing the correct thing, that is, it is subtracting continuously. What is the problem.? – Rajaprabhu Aravindasamy Jan 28 '14 at 12:41
  • @RajaprabhuAravindasamy, I want to subtract the values and get the total value. For example, the first row value is 50000, if the next value is 4000, the total should be 45000, not 54000. If 3000 is entered, the value should 42000, not 57000. I hope you get my point. – input Jan 28 '14 at 12:59
  • @Anton, I'm not typing in negative numbers. – input Jan 28 '14 at 12:59

1 Answers1

1

Updated FIDDLE

Jquery

        function calcSubMTotal(p) {
    var minus = 0;
    var isFirst=true;
    var minus_total = $('#' + p + '');


    $('.' + p + '').each(function (i) {
        if (!isNaN(this.value) && this.value.length !== 0) {
            if(!isFirst)
            minus -= parseInt(this.value); 
            else
            {
                minus=parseInt(this.value);
                isFirst=false;
            }
        }
    });

    minus_total.val(minus);


}

$('input[type=text]').on('keyup', function () {
    calcSubMTotal("rr");
    calcSubMTotal("rr1");
    calcSubMTotal("rr2");
    calcSubMTotal("rr3");
});
P.Sethuraman
  • 705
  • 3
  • 5