On 13/03/2006 13:13, acord wrote:
I want to use document.getElementsByName('name').value to retrieve a
value of the field 'name', but it returns undefined.
Why? Using the forms and elements collections is likely to work across a
wider range of user agents.
But function document.xxxform.name.value returns the correct value.
Notice the plural, '...Elements...'? DOM methods and properties that use
a singular form (such as getElementById) return a single value. Those
that use a plural in their name return a collection (similar to an array).
I thought this 2 functions are identical?
The latter is not a function. It is a series of property accessors.
If getElementsByName is not the same as the later function
It is not. For a start, it returns a collection of elements from across
the entire document, not just a particular form.
is there any other function I can use which is same as
document.xxxform.name.value, but allow me pass in a variable name of
the 'name'?
document.forms.xxxform.elements[myName].value
where myName is such a variable.
[snip]
I suggest you read about bracket notation[1].
Mike
[1] <http://www.jibbering.com/faq/#FAQ4_39>
--
Michael Winter
Prefix subject with [News] before replying by e-mail.