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 2 of 2
  1. #1
    New to the CF scene
    Join Date
    Oct 2008
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Question Variable Scope Help

    I know variables declared out side of a function are global and variables declared inside a function with the var statement are local however variables declared inside a function without the var statement are deemed global. This is simply here to show I tried not to ask for help first!

    I have some code with 3 functions declared as follows:

    Code:
    <SCRIPT type="text/javascript"> <!-- Hide from browsers that do not understand Javascript
    function Loading() {
    alert('Loading');
    document.EditList.EditOption.value = 'Add';
    document.EditList.ModifyName.disabled = true;
    document.EditList.ModifyName.value = '';
    document.EditList.Name.disabled = false;
    document.EditList.Name.value = '';
    document.EditList.Items.value = '';
    document.EditList.Submit.disabled = true;
    count = 0; //Global Variable
    } //Close Loading Function
    
    function Edit() {
    if(document.EditList.EditOption.value == 'Modify') {
    alert(count);
    document.EditList.ModifyName.disabled = false;
    document.EditList.Name.value = '';
    document.EditList.Name.disabled = true;
    } else if(document.EditList.EditOption.value == 'Delete') {
    document.EditList.ModifyName.disabled = false;
    document.EditList.Name.value = '';
    document.EditList.Name.disabled = true;
    } else if(document.EditList.EditOption.value == 'Add') {
    document.EditList.ModifyName.value = '';
    document.EditList.ModifyName.disabled = true;
    document.EditList.Name.disabled = false;
    } //Close Edit Option If Statement
    } //Close Edit Function
    
    function SubCheck() {
    alert(count);
    if (document.EditList.ModifyName.value != '') {
    count = count + 1;
    alert(count);
    } else {
    count = count - 1;
    alert(count); 
    } //Close ModifyName If Statement
    if (document.EditList.Name.value != '') {
    count = count + 1;
    } else {
    count = count - 1;
    } //Close Name If Statement
    if (document.EditList.Pass.value != '') {
    count = count + 1;
    } else {
    count = count - 1;
    } //Close Password If Statement
    if (document.EditList.Items.value != '') {
    count = count + 1;
    } else {
    var count = count - 1;
    } //Close Items If Statement
    if (count == 3) {
    document.EditList.Submit.disabled = false;
    }//Close If Statement
    }// Close Function
    
    //-->
    </SCRIPT>
    Note the redundant alert to display the count variable is for testing only and will be removed later.

    Now to my question. The Loading() function runs properly and declares a global variable of count = 0. I can confirm this when I run my Edit() function it displays the count value in the alert statement as expected. However,
    When I run my SubCheck() function the initial alert windows says the count variable is undefined.

    I will admit this is my first run at implementing Javascript and so far I thought I had a decent understanding. This is not my first exposure to programming as I've implemented VB.NET programs and PHP successfully in other adventures.
    Any help is appreciated.
    Thanks,
    Icy

  • #2
    New to the CF scene
    Join Date
    Oct 2008
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts
    FYI, I accidentally left a var farther down in the code


  •  

    Posting Permissions

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