getElementsByName method (Internet Explorer)

Switch View :
ScriptFree
getElementsByName method

[This documentation is preliminary and is subject to change.]

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

Document Object Model (DOM) Level 3 Core Specification, Section 1.4

Syntax

var retval = document.getElementsByName(v);

Standards information

Parameters

v [in]

Type: BSTR

A String that specifies the value of a NAME attribute.

Return value

Type: ObjectReturns 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.

Examples

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()">

See also

document
About the W3C Document Object Model

 

 

Build date: 2/14/2012

Community Content

Tom Winter - MediQuant
What's a "Collection"
What is the so called "Collection" returned by this method? In IE9 it's a DispHTMLElementCollection. Here's the only docs I could find on this. It's for the HTMLCollection prototype.

http://msdn.microsoft.com/en-us/library/dd347073%28VS.85%29.aspx


LuGerald
Without words
getElementsByName
Returns the (possibly empty) collection of elements whose name value is given by elementName
Parameters
elementName of type DOMString
The name attribute value for an element. $0$0
Return Value NodeList The matching elements.
No Exceptions
Source: http://www.w3.org/TR/2000/WD-DOM-Level-1-20000929/level-one-html.html#ID-71555259
Why do you link to the "DOM Level 1" Standard when your implementation of this function has a completely different behaviour?

Italo Rodrigues
returns matching ids!!!!
Why does this method return elements with matching Ids?

Because it is a collection to index, subindex, and pick, each of its items. Ray.

Corrado75
Doesn't return LABELs
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.

Retropier
Doesn't return SPANs and DIVs
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...)

brindy
Careful when cloning
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.