2

Given Two text Boxes. (TextBox1 & TextBox2), I want add two numbers (using the two text boxes) and show the result in the thrid textbox (TextBox3) instantly i.e without pressing a asp.net button. This is to be done using Javascript. I'm new to javascript so dont have much idea.

This is the asp.net page.

<%@ Page Language="C#" AutoEventWireup="true"  CodeFile="Default.aspx.cs" Inherits="_Default" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title>Untitled Page</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>

        Box1
        <br />
        <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
        <br />
        <br />
        Box2<br />
        <asp:TextBox ID="TextBox2" runat="server"></asp:TextBox>
        <br />
        <br />
        Box3<br />
        <asp:TextBox ID="TextBox3" runat="server"></asp:TextBox>

    </div>
    </form>
</body>
</html>

5 Answers 5

3

First you have to find your textboxes in DOM, then assign event handlers to these elements, and finally you need to write function that will add your numbers. Here is little example:

<script language="JavaScript" type="text/javascript">
 // finding elements. you have to use ASP.NET ClientID property due to ASP generates its own ids.
 var tb1 = document.getElementById('<%= TextBox1.ClientID %>');
 var tb2 = document.getElementById('<%= TextBox2.ClientID %>');
 var tb3 = document.getElementById('<%= TextBox3.ClientID %>');
 // assigning event handlers
tb1.onchange=calcNumbers;
tb2.onchange=calcNumbers;
 // sum function
function calcNumbers() {    
tb3.value = parseInt(tb1.value) + parseInt(tb2.value);
}
</script>
Sign up to request clarification or add additional context in comments.

Comments

1

W- try this (it's all based around the js 'onkeyup' event handler):

[edit 2] - changed event handler yet again to onkeyup. you'd need to do a little check in the sum(0 function that the textbox value was a number (isNan).

<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
    <title>Untitled Page</title>
</head>
<body>
    First Number :<input id="txtFirstNumber" type="text" onkeyup="sum();" /><br />
    Second Number:<input id="txtSecondNumber" type="text" onkeyup="sum();" /><br />
    Third Number:<input id="txtThirdNumber" type="text" /><br />
    <input id="changeWatcher" type="text" />
</body>

<script type="text/javascript">

    function sum() {
        var txtFirstNumberValue = document.getElementById('txtFirstNumber').value;
        var txtSecondNumberValue = document.getElementById('txtSecondNumber').value;
        var result = parseInt(txtFirstNumberValue) + parseInt(txtSecondNumberValue);
        if(!isNaN(result))
        {
            document.getElementById('txtThirdNumber').value = result;
            //alert(parseInt(txtFirstNumberValue) + parseInt(txtSecondNumberValue));
        document.getElementById('changeWatcher').value = new Date();
        }
    }

</script>

</html>

4 Comments

Excellent Jim..It works.... Thank you very much... just another small thing - is there a "change event" or similar script which can be applied so when a value in textbox1 or textbox2 is modified, the textbox3 value is updated instantly - without tabbing or clicking else where. check example here "hotel.coraltours.net" The checkindate and nights date effect the checkout date instantly.
Waqar - that page appears to be using prototype (a javascript framework). however, it's using the onkeyup event, so that may work of you change it in the example.
Thanks Jim and Tafa... Now It works Perfect - Just the way I wanted it.. Cheers Waqar
Glad to have pointed you in the right direction. remember also to mark the correct answer for future searches on the topic. :-)
1

I imagine you can attach a function to each cell's onBlur event to do it.

Comments

0

I am surprised why others use the onBlur event handler. For this problem, I would suggest using the onChange event handler. I may be called just a newbie when javascript is the matter, I am willing to listen why onBlur is preffered here.

2 Comments

tafa - either or, tho i'd agree that the onchange event will give instant results without recourse to leaving the textbox. +1 and have updated my example with credit given to you.
The reason I went with onBlur is so the sum wasn't constantly updating while the user was entering the number. It simply makes things seem a little less "jittery".
-1

And another solution using jQuery function that occurs after you move out of Textbox2 either by mouse clicking or by tabbing forward and it should put the result in Textbox3.

$('#Textbox2').blur(
    function () {
        var t1value = $('#Textbox1').val();
    var t2value = &('#Textbox2').val();
    var sum = t1value + t2value;    
    $('#Textbox3').val(sum);
        });

2 Comments

-1 - This is a simple task that doesn't require jQuery in the least. The OP even states he's new to Javascript...let's show him how to do the simple things without a framework first.
+1 to the comment. but also, if the OP is loading 100s of rows of textboxes jQuery's delegate/live methods would be something of interest here...though I highly doubt that is the case.

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.