Click here to Skip to main content
15,892,005 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
How can add two numbers in two text boxes on third textbox in Scriptpage on web Application?
Posted
Updated 16-Jan-12 18:55pm
v2
Comments
Prerak Patel 17-Jan-12 0:55am    
Any efforts?

XML
<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>
 
Share this answer
 
Comments
Er. Dhaval Panchal 22-Dec-12 5:53am    
Thanx saved my time ..
Er. Dhaval Panchal 22-Dec-12 6:01am    
I want the same for subtraction (-), and multiplication can any one help me?
C#
textbox3.text=textbox1.text+textbox2.text

this is so simple ...........

solution from Black Belt Coder from pakisatan


adnan zaman
 
Share this answer
 
v2
I'd use jquery (jquery.com) even though it seems a very simple thing to do you probably want to be sure the script works in all standard browsers. Also you'll discover some exciting functionality at your fingertips :)

HTML
<input type="text" id="fistBox" value="1">
<input type="text" id="secondBox" value="2">
<nput type="text" id="result" value="">

<script> 
  var theResult =  $("#fistBox").value+ $("#secondBox").value; 
  $('#result').text(theResult);
</script>
 
Share this answer
 
v2
<pre lang="xml">&lt;html xmlns=&quot;http://www.w3.org/1999/xhtml&quot;&gt;
&lt;head runat=&quot;server&quot;&gt;
&lt;title&gt;&lt;/title&gt;
&lt;script type=&quot;text/javascript&quot; src=&quot;http://ajax.googleapis.com/ajax/libs/jquery/1.4.4/jquery.min.js&quot;&gt;&lt;/script&gt;
&lt;script type=&quot;text/javascript&quot;&gt;
$(function () {
var textBox1 = $(&#39;input:text[id$=TextBox1]&#39;).keyup(foo);
var textBox2 = $(&#39;input:text[id$=TextBox2]&#39;).keyup(foo);

function foo() {
var value1 = textBox1.val();
var value2 = textBox2.val();
var sum = add(value1, value2);
$(&#39;input:text[id$=TextBox3]&#39;).val(sum);
}

function add() {
var sum = 0;
for (var i = 0, j = arguments.length; i &lt; j; i++) {
if (IsNumeric(arguments[i])) {
sum += parseFloat(arguments[i]);
}
}
return sum;
}
function IsNumeric(input) {
return (input - 0) == input &amp;&amp; input.length &gt; 0;
}
});
&lt;/script&gt;
&lt;/head&gt;
&lt;body&gt;
&lt;form id=&quot;form1&quot; runat=&quot;server&quot;&gt;
&lt;div&gt;

&lt;table width=&quot;300px&quot; border=&quot;1&quot; style=&quot;border-collapse:collapse;background-color:#E8DCFF&quot;&gt;
&lt;tr&gt;
&lt;td&gt;Butter&lt;/td&gt;
&lt;td&gt; &lt;asp:TextBox runat=&quot;server&quot; ID=&quot;TextBox1&quot;&gt;&lt;/asp:TextBox&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Cheese&lt;/td&gt;
&lt;td&gt;
&lt;asp:TextBox runat=&quot;server&quot; ID=&quot;TextBox2&quot;&gt;&lt;/asp:TextBox&gt;
&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Total&lt;/td&gt;
&lt;td&gt;
&lt;asp:TextBox runat=&quot;server&quot; ID=&quot;TextBox3&quot;&gt;&lt;/asp:TextBox&gt;
&lt;/td&gt;
&lt;/tr&gt;
&lt;/table&gt;
&lt;/div&gt;
&lt;/form&gt;
&lt;/body&gt;
&lt;/html&gt;</pre>
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900