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

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • kaston3

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

    var totalpoints=0;

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

    myvalue=parseFl oat(document.fo rms["myform"].elements[counter].value);
    totalpoints+=my value

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

  • Mick White

    #2
    Re: adding a field value to a var causes a NaN in Explorer

    kaston3 wrote:
    [color=blue]
    > var totalpoints=0;
    >
    > for (counter=1;.... ){
    >
    > myvalue=parseFl oat(document.fo rms["myform"].elements[counter].value);
    > totalpoints+=my value
    >
    > //the fields in elements[] are always numbers
    >
    > }
    >
    > In Internet Explorer the field numbers are not added while in Mozilla
    > everything works ok.[/color]

    Try some troubleshooting


    myvalue=parseFl oat(document.fo rms["myform"].elements[counter].value,10);
    if(isNaN(myvalu e)){alert(myval ue+" is not a number");contin ue;}
    if(!myvalue)){a lert("Empty");c ontinue;}

    totalpoints+=my value

    Mick

    Comment

    • Lee

      #3
      Re: adding a field value to a var causes a NaN in Explorer

      kaston3 said:[color=blue]
      >
      >var totalpoints=0;
      >
      >for (counter=1;.... ){
      >
      >myvalue=parseF loat(document.f orms["myform"].elements[counter].value);
      >totalpoints+=m yvalue
      >
      >//the fields in elements[] are always numbers
      >
      >}
      >
      >In Internet Explorer the field numbers are not added while in Mozilla
      >everything works ok.[/color]

      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+"\myv alue="+myvalue) ;
      should be one of the very first steps in debugging this problem.

      Comment

      • Dr John Stockton

        #4
        Re: adding a field value to a var causes a NaN in Explorer

        JRS: In article <vsQ2f.34688$Xl 2.10659@twister .nyroc.rr.com>, dated
        Tue, 11 Oct 2005 15:00:43, seen in news:comp.lang. javascript, Mick White
        <mwhite13BOGUS@ rochester.rr.co m> posted :[color=blue]
        >
        >myvalue=parseF loat(document.f orms["myform"].elements[counter].value,10);[/color]

        ,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.c om/faq/> JL/RC: FAQ of news:comp.lang. javascript
        <URL:http://www.merlyn.demo n.co.uk/js-index.htm> jscr maths, dates, sources.
        <URL:http://www.merlyn.demo n.co.uk/> TP/BP/Delphi/jscr/&c, FAQ items, links.

        Comment

        Working...