adding two values and dispaly into 3rd textbox [Resolved]

Posted by Ankitsrist under JavaScript on 1/23/2013 | Points: 10 | Views : 33040 | Status : [Member] | Replies : 11
hello,
i have little proble
it's about adding two values from 2 textboxes and displays its value in 3rd textbox without clicking on any button or postbacks
I mean that once the user entered two numbers in two different text boxes, the summation should be added in the third one
my code is as follows

<script language ="javascript" type ="text/javascript" >
function adding()
{
var _txt1=document.getElementById ('<%=TextBox1.ClientID%>');

var _txt2=document.getElementById ('<%=TextBox2.ClientID%>');

var _txt3=document.getElementById ('<%=TextBox3.ClientID%>');

var t1=0, t2=0;

if(_txt1.value!="")

t1=_txt1.value;

if(_txt1.value!="")

t2=_txt2.value;

_txt3.value=parseInt(tl)+parseInt(t2);
}
</script>
enter first no. &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;
&nbsp; &nbsp; &nbsp;&nbsp;
<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox><br />
<br />
enter second no. &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;
&nbsp; &nbsp;
<asp:TextBox ID="TextBox2" runat="server"></asp:TextBox><br />
<br />
result &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp;
<asp:TextBox ID="TextBox3" runat="server"></asp:TextBox>
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp;
</asp:Content>


i know javascript function is not getting call bt when i write onkeyup="adding();" it gives an error that onkeyup is not valid....
help me




Responses

Posted by: Shubham on: 1/23/2013 [Member] Starter | Points: 50

Up
0
Down

Resolved
hello!!! kindly copy and paste this html code
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>Untitled Page</title>


<script TYPE="text/javascript">
function modifyText3()
{
var a = document.getElementById("text1").value;
var b = document.getElementById("text2").value;

var c = parseInt(a)+ parseInt(b);
document.getElementById("text3").value = c;
//document.write("parseInt(a)+b = "+c);
}
</script>




</head>
<body>
<form id="form1" runat="server">
<div>
first number<input id="text1" onchange="modifyText3()" />
Second Number<input id="text2" onchange="modifyText3()" />
result<input id="text3" />

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



Thanks&Regards:-
Shubham Choudhary
Software Engineer
www.facebook.com/shubham.kunar

Ankitsrist, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Shubham on: 1/23/2013 [Member] Starter | Points: 25

Up
1
Down
Hi!!!Ankitsrist


Place an "onchange event" on both your text boxes like so

<input id="text1" onchange="modifyText3" />
<input id="text2" onchange="modifyText3" />
<input id="text3" />
Then your javascript would be

function modifyText3()
{
var val1 = document.getElementById("text1").value;
var val2 = document.getElementById("text2").value;
document.getElementById("text3").value = val1 + val2 + ;
}

Thanks&Regards:-
Shubham Choudhary
Software Engineer
www.facebook.com/shubham.kunar

Ankitsrist, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Ankitsrist on: 1/23/2013 [Member] Starter | Points: 25

Up
1
Down
sir it is again showing an error that onchange is not a valid attribute....wat is the problem with this it is not acceptng onkeypress, onkeyup and ur onchange....do i miss something

Ankitsrist, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Ankitsrist on: 1/23/2013 [Member] Starter | Points: 25

Up
1
Down
thanku sir finaly its done..

Ankitsrist, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Ranjeet_8 on: 1/23/2013 [Member] [MVP] Gold | Points: 25

Up
1
Down
Try this,

<script type="text/javascript" language="javascript">
function Sum() {
var value1 = document.getElementById('<%=TextBox1.ClientID%>').value;
if (value1.length < 1)
value1 = 0;
var value2 = document.getElementById('<%=TextBox2.ClientID%>').value;
if (value2.length < 1)
value2 = 0;
var value3 = parseInt(value1) + parseInt(value2);
document.getElementById('<%=TextBox3.ClientID%>').value = value3;
document.getElementById('<%=TextBox2.ClientID%>').focus();
return false;
}
</script>

 

<asp:TextBox ID="TextBox1" runat="server" MaxLength="10" onchange="return Sum();"
Width="119px"></asp:TextBox>
<asp:TextBox ID="TextBox2" runat="server" MaxLength="10" onchange="return Sum();"
Width="119px"></asp:TextBox>
<asp:TextBox ID="TextBox3" runat="server" MaxLength="10"
Width="119px"></asp:TextBox>


Please Mark As Answer if this helps you

.

Ankitsrist, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Ankitsrist on: 1/23/2013 [Member] Starter | Points: 25

Up
0
Down
it was not working with when i have taken textbox id=textbox1............but when i took input id="text1" it work.....wat is the difference between this

Ankitsrist, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Ankitsrist on: 1/23/2013 [Member] Starter | Points: 25

Up
0
Down
hello Ranjeet_8 sir, i have tried but it is giving an error that "onchange" is not valid, but it is working with html "input" text control

Ankitsrist, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Shubham on: 1/23/2013 [Member] Starter | Points: 25

Up
0
Down
hi!!! Ankitsrist
i m not sure about this but it would be given error due to compatibility of browser and due to textbox runs on server
some time java script not compatible with server controls but some time not always.

Thanks&Regards:-
Shubham Choudhary
Software Engineer
www.facebook.com/shubham.kunar

Ankitsrist, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Ankitsrist on: 1/23/2013 [Member] Starter | Points: 25

Up
0
Down
ok sir thanks again

Ankitsrist, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Ankitsrist on: 1/23/2013 [Member] Starter | Points: 25

Up
0
Down
yeah may be textbox control not support onchange, onkeyup etc

Ankitsrist, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Sgtammana on: 1/25/2013 [Member] Starter | Points: 25

Up
0
Down
Hi,

Try the onblur instead of onchange. then it will work.


Ankitsrist, if this helps please login to Mark As Answer. | Alert Moderator

Login to post response