This page is Almost Ready

Notice: The WebPlatform project, supported by various stewards between 2012 and 2015, has been discontinued. This site is now available on github.

getElementsByName

Summary

Returns an HTMLCollection of elements in the document that have a name attribute with the specified value.

Method of dom/Documentdom/Document

Syntax

var elementList = document.getElementsByName(name);

Parameters

name

Data-type
String

The value of a name attribute.

Return Value

Returns an object of type DOM NodeDOM Node

A list of elements with the a name attribute that matches the specified value.

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 getFirstNames() {
   // Prints a collection with 2 input type=text elements.
   console.log(document.getElementsByName("firstName"));
}
</script>
<input type="text" name="firstName"/>
<input type="text" name="firstName"/>
<input type="button" value="Get Names" onclick="getFirstNames()">

Related specifications

DOM Level 2 HTML
Recommendation
W3C HTML5
Editor’s Draft

Attributions