Click here to Skip to main content
15,889,849 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Dear all,

please help me to find a solution.
I want to add 2 textbox and display result in 3rd one using JAVASCRIPT.

is there any mistake for this code.If yes please provide any solution for me...
C#
<script type="javascript">
function hai()

{
 var num1=document.getElementById(num1).value;
 var num2=document.getElementById(num2).value;;
 var num3=num1+num2;
 alert(num3);
 document.getElementById(result).value = num3;
}</script>
         <INPUT type ="text" id="num1">
<INPUT type = "text" id="num2" onchage="hai()">
<INPUT type = "text" id="result"  READONLY>
Posted
Comments
[no name] 9-Mar-13 13:13pm    
Is there any mistake in your code?
Mike Meinz 9-Mar-13 13:28pm    
Is this your homework? Do you want us to do it for you?
neethujayan 10-Mar-13 10:11am    
yes..its for my project work.

Javascript allows using 3 data types:

String
Number
Boolean

The datatype of your text boxes is String, while to perform any numeric calculation, you must use a Number.
So, you need to convert the contents of the text boxes to a number.

parseInt(num1)

and

parseInt(num2)

Then, you need to do your math and convert the result back to a String using:

result.toString()

So, if 'num3' should display the result of your calculation:

num3=a.toString(parseInt(num1)+parseInt(num2));
 
Share this answer
 
try the code below:
HTML
<input type="text" id="input1" />
<input type="text" id="input2" onchange="doMath();"/>
<input type="text" id='my_sum'  readonly />

<script type="text/javascript">
    function doMath()
    {
        // Capture the entered values of two input boxes
        var input1 = document.getElementById('input1').value;
        var input2 = document.getElementById('input2').value;

        // Add them together and display
        var sum = parseInt(input1) + parseInt(input2);
        document.getElementById('my_sum').value = sum; 
    }
</script>
 
Share this answer
 
v2
Comments
Abhishek Pant 9-Mar-13 14:02pm    
if you want alert of output before the output in textbox then write alert(sum); before the the line document.getElementById('my_sum').value = sum;
neethujayan 10-Mar-13 10:10am    
Ohhh..its working...
Thank you
Abhishek Pant 10-Mar-13 12:10pm    
you can vote and accept if it helped.

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