Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 4 of 4
  1. #1
    New Coder
    Join Date
    Jul 2012
    Posts
    10
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Smile How to get the total as a variable?

    I have a list of questions. For each of them the user inputs a number from 0 - 100. The sum of the three numbers needs to be assigned as a variable. I am going to use that variable in a basic HTML chart where a plugin can change it.

    So my question is from this code:

    Code:
        else if (countId == "Q5" || countId == "Q13" || countId == "Q21") {
            var nFinancial = 0.0;
            nFinancial = parseInt(document.getElementById("Q5").value);
            nFinancial += parseInt(document.getElementById("Q13").value);
            nFinancial += parseInt(document.getElementById("Q21").value);
            tallyTable.rows[0].cells[4].firstChild.data = nFinancial;
        }
    As you can see for this block which is contained in a bigger funtion contains three question (Q5, Q13, and Q21). It functions properly but I need the result as a variable. This is not my code and I thought that nFinancial was the var that represented Q5+Q13+21. However, when I try to alert() the nFinancial var it does nothing when it should default at 0.0.

    Thanks for your time, I can post more of the code if needed but this section was the one giving me the problems.

  • #2
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,567
    Thanks
    78
    Thanked 4,384 Times in 4,349 Posts
    nFinancial *is* the variable.

    But WHERE are you trying to do alert(nFinancial)?

    If the code you show there is inside some function and your alert( ) is NOT inside that same function then it will indeed have no value.
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.

  • #3
    New Coder
    Join Date
    Jul 2012
    Posts
    10
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by Old Pedant View Post
    nFinancial *is* the variable.

    But WHERE are you trying to do alert(nFinancial)?

    If the code you show there is inside some function and your alert( ) is NOT inside that same function then it will indeed have no value.
    Thanks, I sort of figured out what I was doing based on this. Turns out I was expecting an alert() message to pop up as I loaded the page but since it was in an if statement I had to execute that if statement first.

    However, now lets say I want to call the var 'nFinancial' again. I know it works inside of this function where it was named, but is there a way to re-use the function lets say at the end of the page?
    So basically...

    Code:
    <body>
    <script>
    // my entire function goes here, and here is the only place I can use the variables created in the function
    </script>
    //I want

  • #4
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    18,020
    Thanks
    203
    Thanked 2,538 Times in 2,516 Posts
    Declare the variable outside a function to make it global scope, available to any function in the script.

    All the code given in this post has been tested and is intended to address the question asked.
    Unless stated otherwise it is not just a demonstration.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •