6 Replies Latest reply: Aug 12, 2011 9:55 AM by blue72TA RSS

    Getting NaN with Javascript variable

    blue72TA
      Hello All,

      I'm learning javascript and I'm having a problem. I can't seem to use a variable correctly in javascript. The function below shows the problem. When I use the vRow variable my value gets set, however when I try to use link_line variable I get NaN in my tabular field (f37).

      Can anyone spot the problem here? I've tried everything including parseInt, parseFloat conversions but I can't figure out what's wrong.

      Thanks for your help!
      <script>
      function f_ValidateLinkLine(pThis) {
          
         // The row in the table
       var vRow = pThis.id.substr(pThis.id.indexOf('_')+1);
      
        // A value entered by user in tabular form on this row
       var link_line = parseInt(html_GetElement('f20_'+vRow).value);
        
           // Set the value for f37
       
          // This works!
          html_GetElement('f37_'+vRow).value = parseFloat(html_GetElement('f01_'+vRow).value);
      
          // This puts NaN in my field! Why?
          html_GetElement('f37_'+vRow).value = parseFloat(html_GetElement('f01_'+link_line).value);
       
          } // End ValidateLinkLine
      
      </script>
      Edited by: blue72TA on Aug 12, 2011 12:09 AM
        • 1. Re: Getting NaN with Javascript variable
          Zulqarnain
          Hi

          As you may already know NaN means Not a Number. It means the what you are trying to convert using parseFloat or parseInt cannot be converted to number values.

          To investigate please post the output of link_line and html_GetElement('f01_'+link_line).value. You can do this by using alert.
          alert( link_line );
          alert( html_GetElement('f01_'+link_line).value );
          Or if you are using some JavaScript debugging tool like Firebug etc you may also use to find out the values.

          ----
          Zulqarnain
          MaxApex Hosting
          http://www.maxapex.com
          • 2. Re: Getting NaN with Javascript variable
            blue72TA
            Zulqarnain wrote:
            Hi

            As you may already know NaN means Not a Number. It means the what you are trying to convert using parseFloat or parseInt cannot be converted to number values.

            To investigate please post the output of link_line and html_GetElement('f01_'+link_line).value. You can do this by using alert.
            alert( link_line );
            alert( html_GetElement('f01_'+link_line).value );
            Or if you are using some JavaScript debugging tool like Firebug etc you may also use to find out the values.

            ----
            Zulqarnain
            MaxApex Hosting
            http://www.maxapex.com
            Hi,

            Thanks for your response. The value for link_line in this case is 1. It's a line number that the user inputs into the tabular form field. From there I'm trying to get the value of f01 line 1 and put it in f37 on the current row, it's this assignment that fails with the Not a number. Why can't I concatenate with value in var link_line with f01_ to get the value in that field?

            For the alert alert( link_line ) I get 1 and
            for alert( html_GetElement('f01_'+link_line).value ) I get 'undefined'

            Why do I get 'undefined'?


            Thanks!

            Edited by: blue72TA on Aug 12, 2011 6:40 AM

            Edited by: blue72TA on Aug 12, 2011 6:46 AM
            • 3. Re: Getting NaN with Javascript variable
              Joel_C
              Hmmm... Is it possible it's to do with the naming convention for tabular form element IDs?

              I have a feeling that the reason your call to
              alert( html_GetElement('f01_'+link_line).value )
              returns 'undefined' is perhaps because the element you are looking for is actually called something like:
              F01_0001
              Therefore if you are merely concatenating F01_1, naturally I don't think it will find anything with that precise ID (even though semantically, this is what you intend). Try the same test, except hard-code F01_0001 in it's place, I think you'll probably get a mroe fruitful response.
              • 4. Re: Getting NaN with Javascript variable
                blue72TA
                Hi Joel,

                You're right, I need the '000', hard-coding F01_0001 works! I'd hate to admit how long I actually spent on this!

                Thanks for your time!
                • 5. Re: Getting NaN with Javascript variable
                  Joel_C
                  Cool. Glad to have been of some service.

                  I think this is a classic case where working in pairs can really help (obviously not always possible!) - it's the kind of situation where you sit looking at a bit of code for hours, slowly going out of your mind, when a colleague passes by, glances at your screen and says "oh, looks like you're missing a semi-colon +there+"... (facepalm)
                  • 6. Re: Getting NaN with Javascript variable
                    blue72TA
                    Joel_C wrote:
                    Cool. Glad to have been of some service.

                    I think this is a classic case where working in pairs can really help (obviously not always possible!) - it's the kind of situation where you sit looking at a bit of code for hours, slowly going out of your mind, when a colleague passes by, glances at your screen and says "oh, looks like you're missing a semi-colon +there+"... (facepalm)
                    You bet, thanks for taking the time, something was telling me this was a simple fix/oversight, I just needed a fresh pair of eyes on it!