By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
437,636 Members | 1,190 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 437,636 IT Pros & Developers. It's quick & easy.

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

P: n/a
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

Oct 11 '05 #1
Share this Question
Share on Google+
3 Replies


P: n/a
kaston3 wrote:
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

Oct 11 '05 #2

P: n/a
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.

Oct 11 '05 #3

P: n/a
JRS: In article <vs*******************@twister.nyroc.rr.com>, dated
Tue, 11 Oct 2005 15:00:43, seen in news:comp.lang.javascript, Mick White
<mw***********@rochester.rr.com> posted :

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) .

--
John Stockton, Surrey, UK. ?@merlyn.demon.co.uk Turnpike v4.00 IE 4
<URL:http://www.jibbering.com/faq/> JL/RC: FAQ of news:comp.lang.javascript
<URL:http://www.merlyn.demon.co.uk/js-index.htm> jscr maths, dates, sources.
<URL:http://www.merlyn.demon.co.uk/> TP/BP/Delphi/jscr/&c, FAQ items, links.
Oct 11 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.