Click to Rate and Give Feedback
MSDN
MSDN Library
Web Development
HTML and CSS
Methods
 getElementsByName Method

  Switch on low bandwidth view
getElementsByName Method

Gets a collection of objects based on the value of the NAME or ID attribute.

Syntax

collObjects = object.getElementsByName(sNameValue)

Parameters

sNameValue Required. A String that specifies the value of a NAME attribute.

Return Value

Returns a collection of objects with the same NAME attribute value.

Remarks

When you use the getElementsByName method, all elements in the document that have the specified NAME attribute or ID attribute value are returned.

Elements that support both the NAME attribute and the ID attribute are included in the collection returned by the getElementsByName method, but elements with a NAME expando are not included in the collection; therefore, this method cannot be used to retrieve custom tags by name.

Example

This example uses the getElementsByName method to return a collection of input type=text elements with the specified NAME attribute value, firstName.

<SCRIPT>
function fnGetNames(){
   // Returns a collection with 2 INPUT type=text elements.
   var aInput=document.getElementsByName("firstName");
}
</SCRIPT>
<INPUT TYPE="text" NAME="firstName">
<INPUT TYPE="text" NAME="firstName">
<INPUT TYPE="button" VALUE="Get Names" onclick="fnGetNames()">

Standards Information

This method is defined in World Wide Web Consortium (W3C) Document Object Model (DOM) Level 1 World Wide Web link.

Applies To

document, HTMLDocument Constructor

See Also

About the W3C Document Object Model
Tags What's this?: Add a tag
Community Content   What is Community Content?
Add new content RSS  Annotations
Careful when cloning      brindy   |   Edit   |   Show History
If you clone an input element and change the name of that input element, when you call this method it will also return the clones.
Flag as ContentBug
returns matching ids!!!!      FujiFuji   |   Edit   |   Show History
why does this method return elements with matching Ids????
Tags What's this?: Add a tag
Flag as ContentBug
Doesn't return SPANs and DIVs      Retropier   |   Edit   |   Show History
This function does not return elements of type DIV and SPAN with that name, for some reason (unlike in Firefox). So use IDs instead (if you can...)
Tags What's this?: Add a tag
Flag as ContentBug
Doesn't return LABELs      Corrado75   |   Edit   |   Show History
I tried in IE7 on WinXP and it doesn't work for the LABEL elements.
I decided to use getElementsByTagName, which works, and to check the name for each result.
Processing
© 2009 Microsoft Corporation. All rights reserved. Terms of Use  |  Trademarks  |  Privacy Statement
Page view tracker