"Object does not support.." error with document.getElementById("dropdown")

P

Phil Powell

Consider this code:

<code>
<pre>

<html>
<head>
<script type="text/javascript" language="JavaScript">
<!--

function verifyForm(docForm) {
var errMsg = "";
errMsg = verifyFormElements(docForm);

if (errMsg != "") {
alert(errMsg);
return false;
}

return true;
}

function verifyFormElements(docForm) {
var errMsg = "";
var canUseElementById = (docForm.getElementById);

if (canUseElementById && docForm.getElementById("lastname").value ==
"") {
errMsg += "Please enter your last name\\n\\n";
}

if (canUseElementById && docForm.getElementById("firstname").value ==
"") {
errMsg += "Please enter your first name\\n\\n";
}

var dropdown = docForm.getElementById("category");

if (canUseElementById && dropdown.options
[dropdown.selectedIndex].value == "") {
errMsg += "Please choose a catgory\\n\\n";
}

return errMsg;

}
//-->
</script>
</head>
<body>
<form method="post" action="here" onsubmit="return verifyForm(this)">
<table border="0">
<tr>
<td>Last name:</td>
<td><input id="lastname" name="lastname" size="30"></td>
</tr>
<tr>
<td>First name:</td>
<td><input id="firstname" name="firstname" size="30"></td>
</tr>
<td>Category:</td>
<td>
<select id="category" name="category">
<option value="1">Security</option>
<option value="2">IT</option>
<option value="3">Administration</option>
</select>
</td>
</tr>
<tr>
<td colspan="2"><input type="submit" name="submit" value="submit"></
td>
</tr>
</table>
</form>
</body>
</html>

</pre>
</code>

When I try to run it in IE6 (only option allowed by me at this time -
honestly, I am not allowed to install any other browser yet here at
work!) I get the "Object does not allow this property or method" error
on the following line:

var dropdown = docForm.getElementById("category");

Why does this happen? Is this an IE6 hiccup on using ID elements on a
dropdown in Javascript or am I missing something else?

Thanks
 
H

Henry

On Nov 25, 5:55 pm, Phil Powell wrote:
var dropdown = docForm.getElementById("category");

Why does this happen?

The object referred to by - docForm - does not have a - getElementById
- method (it is a FORM element not a document, despite the odd name).
Is this an IE6 hiccup on using ID elements on a
dropdown in Javascript

No, it is a programmer error (or several).
or am I missing something else?

At minimum.
 
P

Phil Powell

On Nov 25, 5:55 pm, Phil Powell wrote:



The object referred to by - docForm - does not have a - getElementById
- method (it is a FORM element not a document, despite the odd name).


No, it is a programmer error (or several).


At minimum.

Care to come up with a solution.. at minimum?
 
G

Gregor Kofler

Phil Powell meinte:
Care to come up with a solution.. at minimum?

Use document.getElementById(). Now that was easy...

Anyway, if you want to improve on your JS abilities (or rather get the
basics right) you should consider to read some documentation first ...
at minimum. Otherwise the next question will inevitable revolve around
IE's odd behaviour with ids and names.

Gregor
 
P

Phil Powell

Phil Powell meinte:







Use document.getElementById(). Now that was easy...

That makes no sense. I am passing document.forms[0] to pass the form
object to get the form ID value; why instead must I use the document
object?
 
G

Gregor Kofler

Phil Powell meinte:
Use document.getElementById(). Now that was easy...

That makes no sense. I am passing document.forms[0] to pass the form
object to get the form ID value; why instead must I use the document
object?

Sigh... Because forms don't have a getElementById() method. Document has.
 
D

Doug Gunnoe

That makes no sense.  I am passing document.forms[0] to pass the form
object to get the form ID value; why instead must I use the document
object?

Try docForm.id

1. You already have the node you need, no need to use getElementById()
 
G

Gregor Kofler

Doug Gunnoe meinte:
That makes no sense. I am passing document.forms[0] to pass the form
object to get the form ID value; why instead must I use the document
object?

Try docForm.id

1. You already have the node you need, no need to use getElementById()

Nope. That won't help to get references to elements within the form. The
form id is of no use for him.

Gregor
 
D

Doug Gunnoe

Nope. That won't help to get references to elements within the form. The
form id is of no use for him.

Gregor

If he is passing a node to his function he already has the node's id,
no need for getElementById().

But yeah, you're right, he doesn't need it.
 

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
473,982
Messages
2,570,190
Members
46,736
Latest member
zacharyharris

Latest Threads

Top