I don't have a problem with people saying something like this:
If your form uses ID's, then you are safer using this syntax.
Or:
For future compatibility, start learning to use this syntax.
I am not so sure that future-compatibility is going to be a worthwhile
argument, depending on which future is being talked about. With XHTML,
currently Mozilla seem very resistant to implementing the HTMLDocument
(from W3C HTML DOM) on the document object in their XHTML DOM (though
they are implementing the rest of the HTML DOM on the objects under it).
Without that interface all the convenience properties of the document
are gone including the forms collection.
Opera 7, OTOH have implemented HTMLDocument on their XHTML document but
I don't think we will know the shape of the XHTML DOM we will be using
until IE produce a browser that implements it (if ever). If they
implement HTMLDocument Mozilla will probably have to give in and follow
suite, if not then the convenience properties will no longer be usable.
In practice the XHTML future is still a long way off and given the
considerable differences between HTML scripting and XHTML scripting
(namespaces probably being the most significant) I don't see much point
in attempting to designing HTML scripts with compatibility with XHTML
DOMs in mind. Probably better to consider the transition to XHTML as a
watershed and leave worrying about the technicalities until XHTML
becomes practical (which means: supported by the majority of browsers,
if not all).
But, to say that its "better" than the other, when used
with a name attribute, is plain wrong.
Personally (and other issues aside) I like the:-
document.forms['myform'].elements['myEl']
- style of accessor for the clarity it brings to the source code.
Looking at it there is no question that the subject of the code is a
form and an element of that form. While a named property of the document
might be an image, an applet, an expando or a non-standard browser
feature. Also, I hadn't formalised my reasons for preferring square
bracket notation where the form and element names are concerned but
Lasse described the practice as keeping the separate namespaces
(HTML/DOM) distinct, which describes my feelings on the subject.
For those reasons I think that the longer form property accessors using
the collections are better, but that is a personal opinion.
... NN4.xx and as long as its around (yes, its still
around), then the ID attribute shouldn't be used with
a Form. Give it a name, access it either way, and move on.
Yes, their is no good excuse for writing code that interacts with forms
in an HTML document in a way that is not compatible with every browser
that understands forms, and certainly all of the browsers in use. It is
probably still the one area of browser scripting where one pattern can
fit all.
<FA ... Y>
Q. How do I acces a form element?
A. If the elements have an ID, then use the forms collection:
document.forms[formID].elements[elementID]
Note: This fails in older browsers.
If the elements have a NAME attribute then you can use
the forms collection or the short cut syntax:
document.forms['formName'].elements['elementName']
or:
document.formName.elementName
</FA ... Y>
Maybe in an expanded 4.13?
Richard:
Is it too late to get something like this incorporated in this
round of revisions to the FAQ? It seems to be coming up a lot
recently.
I don't want to encourage people to inundate me with new FAQ requests at
this point but currently nothing is final and I am happy to entertain
additional suggestions.
Generally I agree with Lasse and think that:-
<draft>
4.nn How do I access a form element?
Named form elements may be referred to as named properties of
the document.forms collection and named form elements as named
properties of the form's elements collection in HTML documents.
document.forms['formName'].elements['elementName']
<link to something with more details on the issues>
</draft>
- might be the most that would be worth putting in the FAQ on the
subject as that will work. The other issues: anonymous forms/elements,
radio button collections, IDs, combining IDs and NAME attributes, XHTML
and getElementById and W3C HTML DOM compliance probably need to be
referred to in other documents else the posted FAQ risks becoming
enormous.
It would be nice to be able to incorporate this in 4.13 because the FAQ
in general is moving away form direct Netscape 4 support and currently
4.13 is specifically about that browser. Unfortunately changing 4.13 too
much is going to break every reference to it in all the articles in the
archives. I don't really want to do that. A compromise might be to make
it more general:-
<draft>
4.13 How do I get the value of a form element?
Named form elements may be referred to as named properties
of the document.forms collection and named form elements as
named properties of the form's elements collection in HTML
documents:
var el=document.forms['formname'].elements['elementname'];
The value property of such elements can be read directly
from the element:-
var value = el.value;
Except when the element is a SELECT element where it is
necessary to read the value property from the currently
selected OPTION element whenever compatibility with older
browsers (like NN 4) may be necessary:-
var value = el.options[el.selectedIndex'].value;
<link to something with more details on the issues>
</draft>
My suggestion for reconciling the need to keep the posted FAQ small and
the desire to provide the necessary detail, caveats and explanation is
to create a second document of notes on the FAQ and link to it from the
FAQ as necessary. I was thinking that whoever edited the FAQ would also
edit that document but anyone who perceived the need to write a note on
any part of the FAQ could contribute to it (even maybe reproduce some of
the more detailed explanations posted to the group (with the authors
permission) as notes). So if anyone feels like contributing an
explanation of the issues around this subject please do so.
Richard.