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"> 
<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>
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"> 
<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>