getElementsByName()

A

acord

Hi,

I want to use document.getElementsByName('name').value to retrieve a
value of the field 'name', but it returns undefined.
But function document.xxxform.name.value returns the correct value.

I thought this 2 functions are identical? If getElementsByName is not
the same as the later function, 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'?
I want to do something like:

var name = 'label'+i
text = document.getElementsByName(name).value

Thanks
A
 
M

Michael Winter

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>
 

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,764
Messages
2,569,564
Members
45,041
Latest member
RomeoFarnh

Latest Threads

Top