0

I want to subtract the values once a user put the value in textfield but unable to do so. Here is an example of sum but I want it same for subtracting:

<table style="border-collapse:collapse;background-color:#E8DCFF" border="1" width="300px">
    <tbody>
        <tr>
            <td width="40px">1</td>
            <td>Butter</td>
            <td><input class="txt" name="txt" type="text"></td>
        </tr>            
        <tr>
            <td>2</td>
            <td>Eggs</td>
            <td><input class="txt" name="txt" type="text"></td>
        </tr>            
        <tr>
            <td>3</td>
            <td>Bread</td>
            <td><input class="txt" name="txt" type="text"></td>
        </tr>
        <tr>
            <td>4</td>
            <td>Soap</td>
            <td><input class="txt" name="txt" type="text"></td>
        </tr>
        <tr id="summation">
            <td> </td>
            <td align="right">Sum :</td>
            <td align="center"><span id="sum">0</span></td>
        </tr>
    </tbody>
</table>

Here is jquery:

<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js"></script>
<script>
    $(document).ready(function(){
        $(".txt").each(function(){
            $(this).keyup(function(){
                calculateSum();
            });
        });
    });
    function calculateSum(){
        var sum = 0;
        $(".txt").each(function(){
            if (!isNaN(this.value) && this.value.length != 0) {
                sum += parseFloat(this.value);
            }
        });
        $("#sum").html(sum.toFixed(2));
    }
</script>
1
  • what kind of problem are you facing? Commented Apr 13, 2015 at 12:20

1 Answer 1

1

You can add a total value, and substract every time you insert something new.

$(document).ready(function(){
	$("#sum").data('total', 600).html(600); // add total value of 600
	$(".txt").each(function(){
		$(this).keyup(function(){
			calculateSum();
		});
	});
});
function calculateSum(){
	var $sum = $("#sum");
	var sum = parseInt($sum.data('total'), 10); // get total value
	$(".txt").each(function(){
		if (!isNaN(this.value) && this.value.length != 0) {
			sum -= parseFloat(this.value);
		}
	});
	$sum.html(sum.toFixed(2));
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js"></script>
<table style="border-collapse:collapse;background-color:#E8DCFF" border="1" width="300px">
    <tbody>
        <tr>
            <td width="40px">1</td>
            <td>Butter</td>
            <td><input class="txt" name="txt" type="text"></td>
        </tr>            
        <tr>
            <td>2</td>
            <td>Eggs</td>
            <td><input class="txt" name="txt" type="text"></td>
        </tr>            
        <tr>
            <td>3</td>
            <td>Bread</td>
            <td><input class="txt" name="txt" type="text"></td>
        </tr>
        <tr>
            <td>4</td>
            <td>Soap</td>
            <td><input class="txt" name="txt" type="text"></td>
        </tr>
        <tr id="summation">
            <td></td>
            <td align="right">Total: </td>
            <td align="center"><span id="sum"></span></td>
        </tr>
    </tbody>
</table>

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

2 Comments

adricadar : thnks for ur help.its working nicely but is it possible to get it without fix value.
@ManindraSingh you mean with pozitive and negative values? What are you trying yo substract?

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.