adding a field value to a var causes a NaN in Explorer

K

kaston3

var totalpoints=0;

for (counter=1;....){

myvalue=parseFloat(document.forms["myform"].elements[counter].value);
totalpoints+=myvalue

//the fields in elements[] are always numbers

}

In Internet Explorer the field numbers are not added while in Mozilla
everything works ok.

// I tried eval and parseint

Thanks
 
L

Lee

kaston3 said:
var totalpoints=0;

for (counter=1;....){

myvalue=parseFloat(document.forms["myform"].elements[counter].value);
totalpoints+=myvalue

//the fields in elements[] are always numbers

}

In Internet Explorer the field numbers are not added while in Mozilla
everything works ok.

Are you sure everything works ok in Mozilla? Is the value of the
first form element included? I'm thinking that the rest of the
line that you replaced with "...." may be important to identifying
your problem.

Since you're taking the extra step of storing the field value in
a temporary variable, adding:
alert("counter="+counter+"\myvalue="+myvalue);
should be one of the very first steps in debugging this problem.
 
M

Mick White

kaston3 said:
var totalpoints=0;

for (counter=1;....){

myvalue=parseFloat(document.forms["myform"].elements[counter].value);
totalpoints+=myvalue

//the fields in elements[] are always numbers

}

In Internet Explorer the field numbers are not added while in Mozilla
everything works ok.

Try some troubleshooting


myvalue=parseFloat(document.forms["myform"].elements[counter].value,10);
if(isNaN(myvalue)){alert(myvalue+" is not a number");continue;}
if(!myvalue)){alert("Empty");continue;}

totalpoints+=myvalue

Mick
 
D

Dr John Stockton

JRS: In article <[email protected]>, dated
Tue, 11 Oct 2005 15:00:43, seen in Mick White
myvalue=parseFloat(document.forms["myform"].elements[counter].value,10);

,10 // ???



IMHO, parseFloat should only be used if the number part of the string
may have training characters; and parseInt only then or if the second
parameter may not be 10. Unless I've forgotten something.

Generally, when reading a number, one should IMHO do something like
myvalue = +document.forms["myform"].elements[counter].value
as it gets the conversion from "outer" to "inner" format out of the way,
and enables the inner value to be checked with a quick alert(myvalue) .
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,434
Messages
2,571,691
Members
48,796
Latest member
Greg L.

Latest Threads

Top