"Michael Winter" <M.******@blueyonder.co.invalid> wrote in message
news:op**************@news-text.blueyonder.co.uk...
<snip>
document.formName.Exp_Month.selectedIndex
<snip>[1] You can also use a collection-oriented accessor method. It is
slightly slower and more to type, but can be used to access ID'd
elements, and elements with names that include characters that
are interpreted as operators in JavaScript (+, -, [, ], *, etc).
The same expression would be written as:
document.forms['formName'].elements['Exp_Month'].selectedIndex
It is not the use of the collections that allows the "illegal"
characters to be used in javascript property accessors it is the square
bracket notation that makes it possible. The property names of an (any)
object are unrestricted by ECMA 262 (probably there are implementation
imposed limits, such as total property name length). It is the dot
notation accessors that impose a restriction on the names used because
the items between the dots must conform to the production rules for an
identifier.
As the two forms of property accessor syntax are equivalent (in terms of
how they work) it is possible to use square bracket notation wherever
dot notation is used, so the above shortcut form accessor could be any
of (and more):-
document["formName"].Exp_Month.selectedIndex
document.formName["Exp_Month"].selectedIndex
document["formName"]["Exp_Month"].selectedIndex
document["formName"]["Exp_Month"]["selectedIndex"]
window["document"]["formName"]["Exp_Month"]["selectedIndex"]
- and the strings used in the bracket notation can hold any character
sequence.
Similarly the collections based assessors could use dot notation
(assuming the names/IDs conformed with the identifier rules):-
document.forms.formName.elements.Exp_Month.selecte dIndex
The reason that I use the longer collections based accessors with the
form and control names in bracket notation is for the clarity it
provides in the source code. The collections access leaves a reader of
the code in no doubt that the subject of the accessor is a control
within a form (as they can clearly read "forms" and "elements" in the
accessor). And the use of bracket notation in that context separates the
HTML namespace from the javascript namespace.
Speed of resolution of the longer accessors is not usually an issue as I
would assign a reference to the - elements - collection to a local
variable anyway:-
var els = document.forms['formName'].elements;
- and then refer to the controls relative to that, so most of the work
resolving the form accessors is only done once. (it also makes the
amount of extra typing involved when using the longer form
insignificant)
Richard.