Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

I would like to add txtbox1+textbox2to get an answer txt3 1

Status
Not open for further replies.

data1

Programmer
Aug 19, 2003
25
NZ
<HTML>
<HEAD>
<TITLE>Untitled</TITLE>
<SCRIPT language=&quot;JavaScript&quot;>function calculate(form1){
f=document.formName

f.t3.value=(f.t1.value-0)+(f.t2.value-0)

}
</SCRIPT>
</HEAD>
<BODY>
<FORM name=&quot;form1&quot;>
<TABLE align=&quot;center&quot; cellspacing=&quot;2&quot; cellpadding=&quot;2&quot; border=&quot;0&quot;>
<TR>
</TABLE>
<TR>
<TD>Inputbox 1
<INPUT type=&quot;text&quot; name=&quot;t1&quot;>
</TD>
<TD>Input box2
<INPUT type=&quot;text&quot; name=&quot;t2&quot;>
</TD>
<TD>Result=
<INPUT type=&quot;text&quot; name=&quot;t3&quot;>
</TD>
<TD>
<INPUT type=&quot;button&quot; value=&quot;Calculate&quot;>
</TD>
</TR>
</TABLE>
</FORM>
</BODY>
</HTML>
 
Hi data1,

Give this a shot.. I also set it to where the result &quot;t3&quot; box would not allow any data to be changed or entered into it.

<HTML>
<HEAD>
<TITLE>Untitled</TITLE>
<SCRIPT language=&quot;JavaScript&quot;>
function calculate(){
var f = document.form1;
var first = f.t1.value;
var second = f.t2.value;
f.t3.value = parseFloat(first) + parseFloat(second);
}
</SCRIPT>
</HEAD>
<BODY>
<FORM name=&quot;form1&quot;>
<TABLE align=&quot;center&quot; cellspacing=&quot;2&quot; cellpadding=&quot;2&quot; border=&quot;0&quot;>
<TR>
</TABLE>
<TR>
<TD>Inputbox 1
<INPUT type=&quot;text&quot; name=&quot;t1&quot;>
</TD>
<TD>Input box2
<INPUT type=&quot;text&quot; name=&quot;t2&quot;>
</TD>
<TD>Result=
<INPUT type=&quot;text&quot; name=&quot;t3&quot; onfocus=&quot;document.form1.t3.blur()&quot; readOnly=&quot;true&quot;>
</TD>
<TD>
<INPUT type=&quot;button&quot; value=&quot;Calculate&quot; onclick=&quot;return calculate()&quot;>
</TD>
</TR>
</TABLE>
</FORM>
</BODY>
</HTML>


Good Luck,
Brian
 
Oh yeah another thing to keep in mind..

If you will be using decimal points and adding your values, you will need to write a function to control the precision of decimal places. (If you will be using IE only you could use the toFixed(x)method [where x is the number of desired decimal places] and place it like so..
f.t3.value = (parseFloat(first) + parseFloat(second)).toFixed(2);)


If you will be using whole numbers simply substitute the parseInt() method for the parseFloat() method I illustrated.

Brian
 
Thanks Brian I think that will work very data1
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top