Sum of two Textbox values into third Textbox using JQuery

A script that sums up two textbox values using jQuery. **Note that I am not using any validation for textbox values.

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>   
    <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.4/jquery.min.js"></script>
    <script type="text/javascript">
        $(function () {
            var textBox1 = $('input:text[id$=TextBox1]').keyup(foo);
            var textBox2 = $('input:text[id$=TextBox2]').keyup(foo);            
 
            function foo() {
                var value1 = textBox1.val();
                var value2 = textBox2.val();               
                var sum = add(value1, value2);
                $('input:text[id$=TextBox3]').val(sum);
            }
 
            function add() {
                var sum = 0;
                for (var i = 0, j = arguments.length; i < j; i++) {
                    if (IsNumeric(arguments[i])) {
                        sum += parseFloat(arguments[i]);
                    }
                }
                return sum;
            }
            function IsNumeric(input) {
                return (input - 0) == input && input.length > 0;
            }
        });
    </script>
</head>
<body>
    <form id="form1" runat="server">
    <div>
 
    <table width="300px" border="1" style="border-collapse:collapse;background-color:#E8DCFF">
    <tr>
        <td>Butter</td>
        <td> <asp:TextBox runat="server" ID="TextBox1"></asp:TextBox></td>
    </tr>
    <tr>
        <td>Cheese</td>
        <td>   
    <asp:TextBox runat="server" ID="TextBox2"></asp:TextBox>
        </td>
    </tr>
    <tr>
        <td>Total</td>
        <td>
    <asp:TextBox runat="server" ID="TextBox3"></asp:TextBox>
        </td>
    </tr>
    </table>   
    </div>
    </form>
</body>
</html>

Output will be as:

Sum

Published Saturday, January 29, 2011 12:38 PM by Rajneesh Verma

Comments

# Sum of two Textbox values into third Textbox using JQuery - Rajneesh Verma's Blog

Pingback from  Sum of two Textbox values into third Textbox using JQuery - Rajneesh Verma's Blog

# re: Sum of two Textbox values into third Textbox using JQuery

Thursday, February 24, 2011 1:45 AM by ishtiaque

excellent tutorial. it helped me alot to complete the project of mine. keep posting similar small tutorial which is very necessary in day to day work of web programming

# re: Sum of two Textbox values into third Textbox using JQuery

Friday, July 08, 2011 11:02 PM by hervz

This code is exceptional. To the author of this code thank you so much. It helps me a lot...

# re: Sum of two Textbox values into third Textbox using JQuery

Saturday, August 20, 2011 3:03 AM by shiva

working good thanks

# re: Sum of two Textbox values into third Textbox using JQuery

Wednesday, November 02, 2011 2:48 AM by sundarpadma

Thanks a lot,, I finish my project with ur coding

# re: Sum of two Textbox values into third Textbox using JQuery

Wednesday, November 02, 2011 2:48 AM by sundarpadma

Thanks a lot,, I finish my project with ur coding

# re: Sum of two Textbox values into third Textbox using JQuery

Friday, November 18, 2011 5:13 AM by sheetal Inani

Its Working... Thank You.. Helped me alot

# re: Sum of two Textbox values into third Textbox using JQuery

Friday, November 25, 2011 11:18 AM by asad

show exact text box of  java

# re: Sum of two Textbox values into third Textbox using JQuery

Wednesday, January 25, 2012 6:19 AM by Kavitha

Thank u so much its very usefull for begining learner like me...

# re: Sum of two Textbox values into third Textbox using JQuery

Thursday, March 01, 2012 12:41 PM by pushkar

If i am trying to run without src="ajax.googleapis.com/.../1.4 it's not working what does it mean please reply.

Regard

pushkar

thanx

# re: Sum of two Textbox values into third Textbox using JQuery

Saturday, March 03, 2012 1:38 AM by Deepak

I need help in finding the Difference of two Textbox values into third Textbox using JQuery or javascript

# re: Sum of two Textbox values into third Textbox using JQuery

Thursday, March 08, 2012 11:03 AM by sam

Best one i ever found. Thanks!

# re: Sum of two Textbox values into third Textbox using JQuery

Thursday, April 05, 2012 3:05 AM by Siva

I am following your code, but i have multiple rows textbox. Pls give idea to do.

Thanks,

Siva.

# re: Sum of two Textbox values into third Textbox using JQuery

Monday, April 30, 2012 2:57 PM by sayanta das

thanks buddy...

# re: Sum of two Textbox values into third Textbox using JQuery

Thursday, June 28, 2012 11:13 AM by suma

thanks so much.

# re: Sum of two Textbox values into third Textbox using JQuery

Tuesday, July 17, 2012 11:35 PM by Leobardo Dorantes

Gracias carnalito se te agradece en verdad es un excelente aporte se que tal vez no me entiendas pero mil gracias

Leave a Comment

(required) 
(required) 
(optional)
(required)