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 SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Newbie Question

Status
Not open for further replies.

camcim

Programmer
Jan 25, 2003
20
0
0
US
Hi Guys,
Why can't i do a simple addition. Basically I'm trying to get values from two input boxes and add them together and place that result into another input box. Multiplication and subtraction seem to work well enough, why not addition?

The code is:

<html>
<head>
<SCRIPT language=JavaScript>
function Sub()
{
var ace1 = document.form1.t1.value;
var ace2 = document.form1.t2.value;
var ace3 = document.form1.t3.value;

var tot = ace1 - ace2;

document.form1.t3.value=tot;
}
</script>
</head>

<body bgcolor=&quot;#FFFFFF&quot; text=&quot;#000000&quot;>
<form name=&quot;form1&quot;>
<input type=&quot;text&quot; name=&quot;t1&quot;><br>
<input type=&quot;text&quot; name=&quot;t2&quot;><br>
<input type=&quot;text&quot; name=&quot;t3&quot;><br>
<input type=&quot;button&quot; name=&quot;Button&quot; value=&quot;Button&quot; onClick=&quot;Sub()&quot;>
</form>
</body>
</html>
 
Yes ... a question that I can answer :)

Just swop this line
var tot = ace1 - ace2;


With this line
var tot = parseInt(ace1) + parseInt(ace2);


You were trying to do maths stuff with a string and in JavaScript the thing you use to combine string is + thats why it did what it did.

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top