...

View Full Version : simple add 2 numbers problem...



disneyfan
03-08-2005, 02:15 PM
what am I missing...


<HTML>
<HEAD>
<TITLE>Help Me</TITLE>
<SCRIPT LANGUAGE="JavaScript 1.2">
<!-- HIDE FROM INCOMPATIBLE BROWSERS
function addNums() {
var num1 = document.testForm.txtNum1.value;
var num2 = document.testForm.txtNum2.value;
var total = num1 + num2;
document.testForm.txtTotal.value = total;
// STOP HIDING -->
</SCRIPT>
</HEAD>
<BODY>
<FORM NAME="testForm">
<P>1st Number:
<INPUT TYPE="text" NAME="txtNum1" SIZE=5></P>
<P>2nd Number:
<INPUT TYPE="text" NAME="txtNum2" SIZE=5></P>
<P>Result:
<INPUT TYPE="text" NAME="txtTotal" SIZE=10></P>
<P>
<INPUT TYPE="button" VALUE="Add" onClick="addNums();">
<INPUT TYPE="reset"></P>
</FORM>
</BODY>
</HTML>

liorean
03-08-2005, 02:36 PM
The value of the form fields is a string, not a number. You are concatenating strings and not adding numbers. Use parseInt or parseFloat to get the numbers from the strings.

disneyfan
03-08-2005, 02:44 PM
thats not all... I'm getting weird errors

it's saying object missing on this line

<INPUT TYPE="button" NAME="button_Add" VALUE="Add" onClick="addNums();">

Brandoe85
03-08-2005, 02:46 PM
Asside from whats allready been pointed out, Use <script type="text/javascript">...your also missing an ending curly brace on your function:


<SCRIPT type="text/javascript">
<!-- HIDE FROM INCOMPATIBLE BROWSERS
function addNums() {
var num1 = document.testForm.txtNum1.value;
var num2 = document.testForm.txtNum2.value;
var total = num1 + num2;
document.testForm.txtTotal.value = total;
}
// STOP HIDING -->
</SCRIPT>

disneyfan
03-08-2005, 02:52 PM
still saying object expected for this line

<INPUT TYPE="button" VALUE="Add" onClick="addNums();">

???

tboss132
03-08-2005, 03:14 PM
Did you take liorean's suggestion? Use parseInt or parseFloat to get the numbers from the floats. If that's still giving you an error, then post your new code. Maybe you are not using them properly
See here (http://www.devguru.com/Technologies/ecmascript/quickref/parseint.html) for a reference.

disneyfan
03-08-2005, 03:29 PM
run the program.. click the add # after entering 2 numbers

<HTML>
<HEAD>
<TITLE>Debugging Problem</TITLE>
<SCRIPT LANGUAGE="JavaScript 1.2">
<!-- HIDE FROM INCOMPATIBLE BROWSERS
function addNums() {
var num1 = parseFloat(document.testForm.txtNum1.value);
var num2 = parseFloat(document.testForm.txtNum2.value);
var total = num1 + num2;
document.testForm.txtTotal.value = total;
}
// STOP HIDING -->
</SCRIPT>
</HEAD>
<BODY>
<FORM NAME="testForm">
<P>1st Number:
<INPUT TYPE="text" NAME="txtNum1" SIZE=5></P>
<P>2nd Number:
<INPUT TYPE="text" NAME="txtNum2" SIZE=5></P>
<P>Result:
<INPUT TYPE="text" NAME="txtTotal" SIZE=10></P>
<P>
<INPUT TYPE="button" VALUE="Add" onClick="addNums();">
<INPUT TYPE="reset"></P>
</FORM>
</BODY>
</HTML>

Brandoe85
03-08-2005, 03:35 PM
Did you change this line:
<SCRIPT LANGUAGE="JavaScript 1.2">
to:
<script type="text/javascript">

As I pointed out in my previous post... :rolleyes:



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum