Forum : variables
Brief description  about Online courses   join in Online courses
View avinash j j 's Profile

variables

<html>
<head>
<SCRIPT language=JAVA SCRIPT>
function calculate(){
var A=document.one.txtFirstnumber.value;
var B=document.txtSecondnumber.value;
A=Number(A);
B=Number(B);
var C=(A B);
document.one.txtThirdnumber.value=C;
}
</SCRIPT>
</head>
<body>
<FORM NAME= one>
1:<INPUT TYPE =text NAME ="txtFirstnumber" SIZE = 5 value ="">
2:<INPUT TYPE =text NAME ="txtSecondnumber" SIZE = 5 value =""><p>
ans: <INPUT TYPE =text NAME ="txtThirdnumber" SIZE = 5 value ="">
<INPUT TYPE= Button Name=b1 VALUE="add them"onclick=calculate()>
</form>
</body>
</html>
could you please let me know what is wrong with it and did not understand this question {concatenate two string, and give space between those stings.}
Asked by avinash j j | Aug 20, 2010 |  Reply now
Replies (2)
View teacher siliconindia 's Profile
Hi Avinash,

<html>
<head>
<SCRIPT language=JAVA SCRIPT> // don't give space betweeen javascript and embed that in between quotes. like, <SCRIPT language="javascript" text="text/javascript">
function calculate(){
var A=document.one.txtFirstnumber.value;
var B=document.txtSecondnumber.value; // mention the form name. syntax is document.formname.fieldname.value
A=Number(A); // no need
B=Number(B); // no need for strings
var C=(A B); // wrong var c = A+B;
document.one.txtThirdnumber.value=C;
}
</SCRIPT>
</head>
<body>
<FORM NAME= one>
1:<INPUT TYPE =text NAME ="txtFirstnumber" SIZE = 5 value ="">
2:<INPUT TYPE =text NAME ="txtSecondnumber" SIZE = 5 value =""><p>
ans: <INPUT TYPE =text NAME ="txtThirdnumber" SIZE = 5 value ="">
<INPUT TYPE= Button Name=b1 VALUE="add them"onclick=calculate()> // give space between HTML tag parameters. Don't use capital letters for tagname and tag parameters and embed the value in b/w quotes.

</form>
</body>
</html>
Aug 23, 2010
View neeraj khatri 's Profile
<html>
<head>
<SCRIPT language=JAVASCRIPT>
function calculate(){
var A=document.one.txtFirstnumber.value;
var B=document.one.txtSecondnumber.value;
var C=(A+B);
document.one.txtThirdnumber.value=C;
}
</SCRIPT>
</head>
<body>
<FORM NAME= one>
1:<INPUT TYPE =text NAME =txtFirstnumber SIZE = 5 value ="">
2:<INPUT TYPE =text NAME =txtSecondnumber SIZE = 5 value =""><p>
ans: <INPUT TYPE =text NAME =txtThirdnumber SIZE = 5 value ="">
<INPUT TYPE= Button Name=b1 VALUE="add them"onClick=calculate()></p>
</form>
</body>
</html>

Try this code :your mistakes are
1.A=Number(A);
B=Number(B); no need to convert them into numbers bcz u are working with strings.

2.onclick=calculate()>-->it should be onClick=calculate()>
the javascript is case sensitive.

3.var C=(A B);-->it should be var C=(A+B).

enjoy.
Aug 21, 2010