PDA

View Full Version : parseInt problem


CdnGal
10-17-2002, 02:18 AM
Hi, all
Can anyone tell me (beginner) what is wrong with this? I need to have the user enter five numbers in the boxes. Then the window alert should TOTAL the numbers that were entered. As it stands right now, it is putting the numbers "together" ie. 12345, but not adding them. I know I need the parseInt for it to see those entries as NUMBERS and not text, but when I put that in, it totally doesn't work. I know I could also write the code another way to add the numbers text boxes, but I MUST have the loop in there, so that has to stay as well. Help? Here's some of the code, plus the file is attached. THANK YOU.

<script language="Javascript">
function totalBoxes() {
for (count=0;count<5;count++){
var x=(document.frm1.elements[count].value)
var sum=sum+x
}
window.alert("The answer is " + sum)
}
</script>
</head>
<body>
<h3>Enter 5 numbers in the boxes below</h3>
<FORM name="frm1">
<input type="text" size="10">&nbsp
<input type="text" size="10">&nbsp
<input type="text" size="10">&nbsp
<input type="text" size="10">&nbsp
<input type="text" size="10"><br>
<input type="button" value="Add up text boxes" onClick=totalBoxes()>
</form>

glenngv
10-17-2002, 02:26 AM
function totalBoxes() {
var sum=0;
for (count=0;count<5;count++){
var x=(document.frm1.elements[count].value)
var sum+=parseInt(x)
}
window.alert("The answer is " + sum)
}

CdnGal
10-17-2002, 02:59 AM
Thank you, Glenn...except now I'm getting an error at line 23, saying "object expected" which is my line where I have the following:

<input type="button" value="Add up text boxes" onClick="totalBoxes()">

any ideas? I tried it with and without the quotation marks, and get the same error message.

Thank you so much for your help.

glenngv
10-17-2002, 03:17 AM
can you post the whole code?

CdnGal
10-17-2002, 03:19 AM
Here it is.....

<html>
<head>
<title>Question 2 - Add 5 Text Boxes</title>
<script language="Javascript">
function totalBoxes() {
var sum=0
for (count=0;count<5;count++){
var x=(document.frm1.elements[count].value)
var sum+=parseInt(x)
}
window.alert("The answer is " + sum)
}
</script>
</head>
<body>
<h3>Enter 5 numbers in the boxes below</h3>
<FORM name="frm1">
<input type="text" size="10">&nbsp
<input type="text" size="10">&nbsp
<input type="text" size="10">&nbsp
<input type="text" size="10">&nbsp
<input type="text" size="10"><br>
<input type="button" value="Add up text boxes" onClick=totalBoxes()>
</form>
</body>
</html>

chrismiceli
10-17-2002, 03:24 AM
why do you have
x=(document.etc)
why not just
x=document.etc.

glenngv
10-17-2002, 03:27 AM
<html>
<head>
<title>Question 2 - Add 5 Text Boxes</title>
<script language="Javascript">
function totalBoxes() {
var sum=0
for (count=0;count<5;count++){
var x=document.frm1.elements[count].value
sum+=parseInt(x)
}
window.alert("The answer is " + sum)
}
</script>
</head>
<body>
<h3>Enter 5 numbers in the boxes below</h3>
<FORM name="frm1">
<input type="text" size="10">
<input type="text" size="10">
<input type="text" size="10">
<input type="text" size="10">
<input type="text" size="10"><br>
<input type="button" value="Add up text boxes" onClick="totalBoxes()">
</form>
</body>
</html>

CdnGal
10-17-2002, 03:28 AM
ok, I took that out (just put it in for tidiness) but I still get the same error.

CdnGal
10-17-2002, 03:30 AM
You are a god! Thank you sooooooooo much for your help!!!

glenngv
10-17-2002, 03:31 AM
if you copy and paste it, it runs just fine, it works for me.

CdnGal
10-17-2002, 03:37 AM
yes it does! Thank you. (our messages got crossed over the wires when I said it didn't work)

Thank you again! I have been picking apart that code for HOURS and probably would have spent many more on it had you not helped me!

I really appreciate it!