please help (newbie problem...) "document.myForm has no properties"

A

aotemp

I am using javascript and firefox (with the wonderful javascript
console).

within the body of the document I have defined a function that
references an object on my form with the statement

"document.myForm.mySelect"

however when the browser sees this line, it spits out the following
error into the console, and does not execute the function...

"document.myForm has no properties"

defined later in the body as

<form name="myForm" onsubmit="myFunction()">....</form>

I did a search in the group for an answer and did not find the
information that I needed...

thanks for the help!
 
D

Dietmar Meier

"document.myForm has no properties"

defined later in the body as

<form name="myForm" onsubmit="myFunction()">....</form>

At the moment your statement is executed the browser doesn't know
yet what you've defined later in the body. Either place the script
after the form in the document, or (better) execute the script
onload. In any case you should test if objects and methods you
want to use are present:

window.onload = function() {
if (document.forms["myForm"]
&& document.forms["myForm"].elements["mySelect"]
) {
// now do something with
// document.forms["myForm"].elements["mySelect"] ...
}
}

or better

window.onload = function() {
var oForm, oSelect;
if ((oForm = document.forms["myForm"])
&& (oSelect = oForm.elements["mySelect"])
) {
// now do something with oSelect ...
}
}

ciao, dhgm
 
R

RobB

Dietmar said:
At the moment your statement is executed the browser doesn't know
yet what you've defined later in the body.

(snip)

But he did say the object reference was in that function, not called
until Form.onsubmit runs. Some details are missing here...
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,432
Messages
2,571,682
Members
48,796
Latest member
Greg L.

Latest Threads

Top