Flash Website Builder- Trendy Site Builder is a Flash Site Building tool that helps users build stunning websites. Check Out Custom Custom Logo Design by LogoBee. Website Design and Free Logo Templates available.
 CodingForums.com Can't change Value of a Global Variable

Before you post, read our: Rules & Posting Guidelines

Enjoy an ad free experience by logging in. Not a member yet? Register.
 09-21-2012, 07:57 PM PM User | #1 greenhat New Coder   Join Date: Sep 2012 Posts: 10 Thanks: 9 Thanked 0 Times in 0 Posts Can't change Value of a Global Variable I need help with this calculate script. In the first function, I cannot seem to assign a value to the global variable grandTotal for whatever reason. I want a span with id grandTotal to display it. If change it to a string it works. So i assigned a the value from the function to it. How do I either get the global variable to accept it or add the total from the second function calculateChurch() total to it and display an updated total? Code: ```
 09-21-2012, 08:36 PM PM User | #2 Philip M Supreme Master coder!     Join Date: Jun 2002 Location: London, England Posts: 17,494 Thanks: 200 Thanked 2,471 Times in 2,449 Posts You are using grandTotal both as the name of an HTML element and a global Javascript variable. That will cause IE to barf. Same with var LHtotal; var AHtotal; var LMtotal; var AMtotal; But the big problem is var orgTotal = orgTotal.toFixed(2); grandTotal = Number(schoolTotal + orgTotal); .toFixed() converts the numeric value to a string so the following + sign concatenates, not adds the values. You should use toFixed() only to make a value for display purposes. It cannot be used for any subsequent mathematical calculation. If you need to retain the value as a number use Code: `````` Quizmaster: Do you know what 20 per cent of 100 is? Contestant: 20 per cent of 100 is five per cent __________________ 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.
 Users who have thanked Philip M for this post: greenhat (09-28-2012)
 09-21-2012, 10:22 PM PM User | #3 greenhat New Coder   Join Date: Sep 2012 Posts: 10 Thanks: 9 Thanked 0 Times in 0 Posts Should not be this hard. All i am trying to do is get a total and update an element
 09-22-2012, 12:20 AM PM User | #4 Old Pedant Supreme Master coder!     Join Date: Feb 2009 Posts: 24,965 Thanks: 75 Thanked 4,309 Times in 4,276 Posts Code: ``` var orgTotal = (Ototal + Etotal); // the var on the next line is BOGUS...you can't use var for the same name twice // but in any case GET RID of this next line COMPLETELY: /* var orgTotal = orgTotal.toFixed(2); */ grandTotal = Number(schoolTotal + orgTotal); if(isNaN(grandTotal)) { grandTotal = ""; } else { grandTotal = grandTotal.toFixed(2); } if(isNaN(orgTotal)) { orgTotal = ""; } else { orgTotal = orgTotal.toFixed(2); } document.getElementById("orgTotal").innerHTML = orgTotal; document.getElementById(orgType + 'total').innerHTML = total; document.getElementById(orgType + 'active').innerHTML = active; document.getElementById("schoolTotal").innerHTML = schoolTotal; // document.getElementById("grandTotal").innerHTML = schoolTotal; ??? SURELY THIS IS WRONG ??? // shouldn't it be document.getElementById("schoolTotal").innerHTML = grandTotal;``` red means kill it blue means add it __________________ 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.
 Users who have thanked Old Pedant for this post: greenhat (09-28-2012)
 09-22-2012, 01:16 AM PM User | #5 greenhat New Coder   Join Date: Sep 2012 Posts: 10 Thanks: 9 Thanked 0 Times in 0 Posts Still no good. The else statements cause it not to display the value at all. I got rid of to.fixed(). document.getElementById("grandTotal").innerHTML only shows if I: Change it to a string change it to schoolTotal That is fine for the first function for it to show schoolTotal. When the second function runs, the value is not replaced with the updated total. In, fact churchTotal will not display even in another element.
 09-22-2012, 01:28 AM PM User | #6 Old Pedant Supreme Master coder!     Join Date: Feb 2009 Posts: 24,965 Thanks: 75 Thanked 4,309 Times in 4,276 Posts Maybe you will have to show us the actual page. Anyway, this is bogus: Code: ```document.getElementById("grandTotal").innerHTML = document.getElementById("grandTotal").innerHTML + churchTotal;``` That will *NOT* add the NUMBERS. It will append the strings. Maybe you could use Code: ```document.getElementById("grandTotal").innerHTML = Number(document.getElementById("grandTotal").innerHTML) + churchTotal;``` BUt I'm just having a hard time following the logic of the code. I don't see why you need the isNaN() checks in there. When will the values being checked ever NOT be numbers?? __________________ 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.
 Users who have thanked Old Pedant for this post: greenhat (09-28-2012)
 09-22-2012, 01:39 AM PM User | #7 greenhat New Coder   Join Date: Sep 2012 Posts: 10 Thanks: 9 Thanked 0 Times in 0 Posts Html here is the HTML right now HTML Code: ```

Try Our Calculator!

Secondary Schools # of School Fundraisers # of Enrolled % Participating No. Students on Clubs/Teams Gift Baskets Sold \$26 Avg Whsle Price Fundraisers Per Year Commission Your Total Commission
Large High Schools (55 Clubs/Teams) 2400+ Students x [ x = ] x x\$26.00 x x6% =
Avg High Schools (40 Clubs/Teams) 855+ Students x [ x = ] x x \$26.00 x x 6%  =
Large Middle Schools (55 Clubs/Teams) 1855+ Students x [ x = ] x x \$26.00 x x 6%  =
Avg Middle Schools (40 Clubs/Teams) 650+ Students x [ x = ] x x \$26.00 x x 6%  =
Total Commissions From Schools

Calculator Example of a Church

Churches # of Churches # of Members % Participating Gift Baskets Sold \$26 Avg Wholesale Price Fundraisers Per Year 6% Commission Your Total Commission
Large Churches (1000+ Members) x x x x \$26.00 x 6% =
Avg Churches (100-999 Members) x x x x \$26.00 x x6% =
Total Commissions From Churches

Calculator Example of a Church

Secondary Schools # of School Fundraisers # of Enrolled % Participating No. Students on Clubs/Teams Gift Baskets Sold \$26 Avg Whsle Price Fundraisers Per Year Commission Your Total Commission
Elementary Schools(PTO Others) 500+ x x x x\$26.00 x x6% =
Organizations x x x x \$26.00 x x 6%  =
Total Commissions From Schools

Total Number of commissions from schools, churches and organizations: \$

```
 09-22-2012, 02:03 AM PM User | #8 Old Pedant Supreme Master coder!     Join Date: Feb 2009 Posts: 24,965 Thanks: 75 Thanked 4,309 Times in 4,276 Posts Sorry. I don't understand the logic of it. Your button says "Calculate" under "Total commission for schools", but then the only thing you look at/work on is "LH" school line, ignoring the other lines. Just doesn't make sense to me. __________________ 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.
 Users who have thanked Old Pedant for this post: greenhat (09-28-2012)

 Bookmarks

 Thread Tools Rate This Thread Rate This Thread: 5 : Excellent 4 : Good 3 : Average 2 : Bad 1 : Terrible

 Posting Rules You may not post new threads You may not post replies You may not post attachments You may not edit your posts BB code is On Smilies are On [IMG] code is On HTML code is Off Forum Rules
 Forum Jump User Control Panel Private Messages Subscriptions Who's Online Search Forums Forums Home :: Client side development     JavaScript programming         DOM and JSON scripting         Ajax and Design         JavaScript frameworks         Post a JavaScript     HTML & CSS     XML     Flash & ActionScript         Adobe Flex     Graphics and Multimedia discussions     General web building         Site reviews         Building for mobile devices :: Server side development     Apache configuration     Perl/ CGI     PHP         Post a PHP snippet     MySQL         Other Databases     Ruby & Ruby On Rails     ASP     ASP.NET     Java and JSP     Other server side languages/ issues         ColdFusion         Python :: Computing & Sciences     Computer Programming     Computer/PC discussions     Geek News and Humour Web Projects and Services Marketplace     Web Projects         Small projects (quick fixes and changes)         Medium projects (new script, new features, etc)         Large Projects (new web application, complex features etc)         Unknown sized projects (request quote)         Vacant job positions         Looking for work/ for hire         Project collaboration/ partnership         Paid work offers and requests (Now CLOSED)     Career, job, and business ideas or advice     Domains, Sites, and Designs for sale         Domains for sale         Websites for sale         Design templates and graphics for sale :: Other forums     Member Offers     Forum feedback and announcements

All times are GMT +1. The time now is 06:47 PM.