JavaScript return NaN

Discussion in 'JavaScript' started by jellybeannn, May 27, 2010.

  1. #1
    Hi,

    I've got this function when values are entered into two TextBox's, they are multiplied and this is displayed in a label("amount"), what I want to do is to add all the "amount" labels and display it in another label as the user goes along. But the label2 displays NaN, I know this means not a number, but only numbers where entered.

    
    function Calculate(textbox1, textbox2, label1, label2)
        {
                var txt1 = 0;
                var txt2 = 0;
                
                txt1 = document.getElementById(textbox1).value;
                if (txt1 == "")
                    txt1 = 0;
                
                txt2 =  document.getElementById(textbox2).value;
                if (txt2 == "")
                    txt2 = 0;
    
                var cost = txt1 * txt2;
                document.getElementById(label1).innerHTML = cost;
    
                for (i = 1 ;i <= 15; i++)
                {
                    var amount = (document.getElementById('amount'+i).value);
                }
                document.getElementById(label2).innerHTML = eval(total += amount);
        }
    
    Code (markup):

     
    jellybeannn, May 27, 2010 IP
  2. shubhamjain

    shubhamjain Active Member

    Messages:
    215
    Likes Received:
    2
    Best Answers:
    1
    Trophy Points:
    63
    #2
    Try this but I am not sure it will work or not.
     
    shubhamjain, May 27, 2010 IP
  3. trevHCS

    trevHCS Peon

    Messages:
    40
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #3
    Suspect by the looks of that you've got a few problems, but essentially the NaN is being caused by the "total" var not existing. But going backwards through the problems:


    1) This loop doesn't appear to be doing anything useful. Currently it's saying loop through the contents of a series of elements (maybe textboxes?) called amount1, amount2 ... amount15 but on each increment it changes the value of the "var amount" to the next one. So in the end the variable "amount" only ends up with the value of element ID "amount15" (textbox or whatever it is).

    I suspect what you meant to do is say "var amount += ...." to add all the values together?

    for (i = 1 ;i <= 15; i++)
    {
     var amount = (document.getElementById('amount'+i).value);
    }
    
    Code (markup):

    2) Here we suddenly find a variable called "total" which doesn't appear to be declared anywhere with any value thus you're trying to add [nothing] to the value of "amount". Javascript doesn't like that partly because the variable "total" has appeared from nowhere and partly because it's not a number.

    Hence it's throwing these errors saying the result of this isn't a number at all.

    Also, why are you running eval() across that? Really thats intended for evaluating chunks of code as if they're J/script, so doesn't seem to really have any purpose here.

    document.getElementById(label2).innerHTML = eval(total += amount);
    Code (markup):
    Trev
     
    trevHCS, May 27, 2010 IP
  4. Lord

    Lord Active Member

    Messages:
    134
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    53
    #4
    just put
    total=0;
    Code (markup):
    in the beginning of the code

    if everything else is ok then this could be the problem
     
    Lord, May 27, 2010 IP