By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
459,689 Members | 1,901 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 459,689 IT Pros & Developers. It's quick & easy.

Variable Scope

P: n/a
If I do . . .

myForm=document.tstForm;
function initialSetup(){
myForm.fld01.value="Test 01"
myForm.fld02.value="Test 02";
myForm.fld01.focus();
}

Then, in the body tag, I do onLoad="initialSetup()",
the script doesn't work and I get a "myForm has no properties" error

I know it'll work if I move it within the function, but I figured a
global variable would retain its value within the function. Why not?
Jul 20 '05 #1
Share this Question
Share on Google+
2 Replies


P: n/a
This would be because at this point

myForm=document.tstForm;

the form is not created - you will need to initialise this var after page
loads

"Jason Cook" <ja*********@hotmail.com> wrote in message
news:d8**************************@posting.google.c om...
If I do . . .

myForm=document.tstForm;
function initialSetup(){
myForm.fld01.value="Test 01"
myForm.fld02.value="Test 02";
myForm.fld01.focus();
}

Then, in the body tag, I do onLoad="initialSetup()",
the script doesn't work and I get a "myForm has no properties" error

I know it'll work if I move it within the function, but I figured a
global variable would retain its value within the function. Why not?

Jul 20 '05 #2

P: n/a
Forgot to add -

you can declare the global var

var myForm

outside the function, and then set it calue inside the function

function init(){
myForm = document.tstForm;
}

"Jason Cook" <ja*********@hotmail.com> wrote in message
news:d8**************************@posting.google.c om...
If I do . . .

myForm=document.tstForm;
function initialSetup(){
myForm.fld01.value="Test 01"
myForm.fld02.value="Test 02";
myForm.fld01.focus();
}

Then, in the body tag, I do onLoad="initialSetup()",
the script doesn't work and I get a "myForm has no properties" error

I know it'll work if I move it within the function, but I figured a
global variable would retain its value within the function. Why not?

Jul 20 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.