...

View Full Version : Variable Scope Help



Icyhot
10-05-2008, 04:11 AM
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:


<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

Icyhot
10-05-2008, 09:27 PM
FYI, I accidentally left a var farther down in the code



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum