• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Modifying global variables

 
Ranch Hand
Posts: 53
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi, I have a javascript shopping cart that has a function which adds items and all of their properties to the cart. The function checks for a form field called TAXABLE. If it exists, it then "ATTEMPTS" to changes the value of a global variable like this...
var isTaxable = false; // global variable
function addItem(myForm)
{
if (myForm.TAXABLE == null)
isTaxable = false; //change global value
else
isTaxable = true; //change global value
}
This, for some reason, doesn't change the global variable isTaxable. I am so confused. Please help me change the global variable so that it depends on the addItem function above.
 
Sheriff
Posts: 67746
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
What you are trying to do is straight-forward and there's no reason it shouldn't work.
Are you sure the function is being called? Are you sure that myForm is not null? Are you sure that some other JavaScript error is not occurring that prevents the statements from executing?
Debugging in Mozilla with the JavaScript console open is a great way to find such problems.
hth,
bear
P.S. Please use the UBB code tags around posted code. makes it a lot easier to read.
 
author
Posts: 15385
6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I thinik you want the value, so you would want to use
document.FormName.ElementName.value
 
Bear Bibeault
Sheriff
Posts: 67746
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

I thinik you want the value, so you would want to use


Actually, that was my first reaction too, Eric. But I believe (correct if wrong) that he's interested in whether the control exists or not -- not whether it has a non-empty value.
So I think his check should work as coded. Your thoughts?
Michael, have you diagnosed this further?
bear
[ July 11, 2003: Message edited by: Bear Bibeault ]
 
Eric Pascarello
author
Posts: 15385
6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
if that was the case I would just use
if(myForm.TAXABLE) //would exsist
else //is not there
 
Michael McNally
Ranch Hand
Posts: 53
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I'm actually very stuck. Basically speaking, we have two function scopes and a global scope that we must work around.
-In the global scope, I have declared a property named isTaxable = false;
-Then, in the AddToCart() function scope (which is the scope that collects data from my form) I asked the interpreter if a form field named TAXABLE exists. And if it does, the value of my global window property changes to true.

Finally, in another function scope named ManageCart(), I want to change my tax variable(fTax) to depend on the value of isTaxable, like so...

Unfortunately, isTaxable is always false in the global scope. Though, I have tested the function that changes isTaxable to true and in that function scope it works. If you want to see the script I am talking about to get an understanding of it, you may download it from http://www.statuniform.com/javascript/nopcart.js Though it does not contain any of the code we are talking about here because it is on a live shopping cart system.
[ July 12, 2003: Message edited by: Michael McNally ]
 
Michael McNally
Ranch Hand
Posts: 53
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The way I see it, isTaxable should be true in all scopes if the form.TAXABLE property exists. Correct?
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic