Forum guidelines
AuthorQuestion
Calculation in text box
Posted By Prasanta Baidya on 08 Feb 2013
I want to minus two value live in the page on the basis of the following jquery, I have sum code it is work properly. Please help.

my code is:

<table width="300px" border="1" style="border-collapse: collapse; background-color: #E8DCFF">
        <tr>
            <td width="40px">
                1
            </td>
            <td>
                Number
            </td>
            <td>
                <input class="txt" type="text" name="txt" runat="server" />
            </td>
        </tr>
        <tr>
            <td>
                2
            </td>
            <td>
                Number
            </td>
            <td>
                <input class="txt" type="text" name="txt" runat="server" />
            </td>
        </tr>
        <tr>
            <td>
                3
            </td>
            <td>
                Number
            </td>
            <td>
                <input class="txt" type="text" name="txt" runat="server" />
            </td>
        </tr>
        <tr>
            <td>
                4
            </td>
            <td>
                Number
            </td>
            <td>
                <input class="txt" type="text" name="txt" runat="server" />
            </td>
        </tr>
        <tr>
            <td>
                5
            </td>
            <td>
                Number
            </td>
            <td>
                <input class="txt" type="text" name="txt" runat="server" />
            </td>
        </tr>
        <tr>
            <td>
                6
            </td>
            <td>
                Number
            </td>
            <td>
                <input class="txt" type="text" name="txt" runat="server" />
            </td>
        </tr>
        <tr id="summation">
            <td>&nbsp;
               
            </td>
            <td align="right">
                Sum :
            </td>
            <td align="center">
                <span id="sum">0</span>
            </td>
        </tr>
    </table>

</body>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.5.0/jquery.min.js"></script>

    <script>
        $(document).ready(function () {
            //iterate through each textboxes and add keyup        
            //handler to trigger sum event        
            $(".txt").each(function () {
                $(this).keyup(function () {
                    calculateSum();
                });
            });
        });
        function calculateSum() {
            var sum = 0;
            //iterate through each textboxes and add the values
            $(".txt").each(function () {
                //add only if the value is number            
                if (!isNaN(this.value) && this.value.length != 0) {
                    sum += parseFloat(this.value);
                    $(this).css("background-color", "#FEFFB0");
                }
                else if (this.value.length != 0) {
                    $(this).css("background-color", "red");
                }
            });
            $("#sum").html(sum.toFixed(2));
        }
    </script>

AuthorReply
Re: Calculation in text box
Posted By Dorababu on 08 Feb 2013  
I think this is the one you have written from

http://viralpatel.net/blogs/demo/sum-textbox-value-javascript.html

you can see the working demo here 

http://viralpatel.net/blogs/demo/sum-textbox-value-javascript.html

Replace your 

<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.5.0/jquery.min.js"></script
with

<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js"></script



SPONSORED BY

Custom Software Development
MCN is your source for developing solutions involving websites, mobile apps, cloud-computing, databases, BI, back-end services and processes and client-server applications.