Randy said:
Gérard Talbot said the following on 9/9/2005 9:52 PM:
crjunk a écrit :
I have a script that I want to run only when my input
box IS NOT disabled. Can someone tell me if something
is wrong with my script? Is "disabled" the correct
property to use?
function TextChanged(i){
if (!document.ScheduleForm["txtGrossPayroll" + i].disabled) {
if (!document.ScheduleForm.elements.namedItem(
"txtGrossPayroll + i).disabled) {
Forms are a collection, and as such, should be accessed via
the Array syntax - [].
Well, they should be accessed with property accessors. Bracket notation
is only necessary when the names used do not qualify as Identifiers.
Though bracket notation is recommended for the task regardless of the
names in order to highlight the distinction between names originating
within JS and the DOM and names that originate in mark-up elements, and
to conform with the W3C HTML DOM standard (as explained below).
So, the original code ...["txt...]. was correct.
Functional, yes. Correct is more a matter of opinion. I would quibble
about the use of the 'shortcut' of referencing the form object as a
named property of the document. Because while no HTML DOM has been
identified as not making forms available as properties of the document
we know that some XHTML DOMs only support the W3C HTML DOM standard
method of accessing forms as properties of the - document.forms -
collection, and using the - document.forms - collection works on all
browsers that allow the 'shortcut'. This makes the W3C DOM standard
property accessors the most widely supported and general approach (being
equally viable in all environments that expose forms to scripting, past
present and (theoretically) future).
Using ()
for form elements is an IE only way of accessing them.
<snip>
It is, but that is not relevant here as the function being called is
the - namedItem - method defined as a method of the HTMLCollection
interface in the W3C DOM standard. The problem with the use of this
method is that the older browsers providing pre-W3C HTML DOM access to
forms do not necessarily implement the - namedItem - method. That is not
actually an issue when writing ECMAScritp because the ECMAScript
bindings for the HTMl DOM says:-
<quote cite="
http://www.w3.org/TR/2003/
REC-DOM-Level-2-HTML-20030109/ecma-script-binding.html">
Functions of objects that implement the HTMLCollection interface:
item(index)
This function returns an object that implements the Node
interface.
The index parameter is a Number.
Note: This object can also be dereferenced using square
bracket notation (e.g. obj[1]). Dereferencing with an integer
index is equivalent to invoking the item function with that
index.
namedItem(name)
This function returns an object that implements the Node
interface.
The name parameter is a String.
Note: This object can also be dereferenced using square
bracket notation (e.g. obj["foo"]). Dereferencing using a
string index is equivalent to invoking the namedItem function
with that index.
</quote>
And so because the specification requires that the - item - and -
namedItem - methods are effectively mapped to bracket notation property
accessors there is never any need to use the methods directly, and so no
reason to limit cross-browser compatibility through their use. A fully
W3C HTML DOM standard conforming property accessor for the form control
would go:-
document.forms['ScheduleForm'].elements[("txtGrossPayroll + i)]
- and still be fully functional with all of the browsers that have ever
exposed forms to javascript (and, because it is HTML DOM standard, can
reasonably be expected to be equally functional on future HTML DOM
standard browsers).
Richard.