Document.getElementsByName()

  • Revision slug: Web/API/document.getElementsByName
  • Revision title: document.getElementsByName
  • Revision id: 447971
  • Created:
  • Creator: kscarfone
  • Is current revision? No
  • Comment Editorial review

Revision Content

{{DomRef}}

Summary

Returns a list of elements with a given {{domxref("element.name","name")}} in the (X)HTML document.

Syntax

elements = document.getElementsByName(name) 
  • elements is an {{domxref("HTMLCollection")}} of elements.
  • name is the value of the name attribute of the element.

Example

<!DOCTYPE html>
<html lang="en">
<head>
 ...
</head>

<body>
<form name="up"><input type="text"></form>
<form name="down"><input type="text"></form>

<script>
var up_forms = document.getElementsByName("up");
w(up_forms[0].tagName); // returns "FORM"
</script>
</body>
</html>

Notes

The name attribute is only applicable to (X)HTML documents. The method returns all elements with a name attribute, such as {{htmlelement("meta")}} or {{htmlelement("object")}} or even if name is placed on elements which do not support a name attribute at all.

document.getElementsByName() returns an {{domxref("HTMLCollection")}} of all the elements with a given value for the name attribute.

Specifications

Revision Source

<div>
  {{DomRef}}</div>
<h2 id="Summary" name="Summary">Summary</h2>
<p>Returns a list of elements with a given {{domxref("element.name","name")}} in the (X)HTML document.</p>
<h2 id="Syntax" name="Syntax">Syntax</h2>
<pre class="syntaxbox">
<em>elements</em> = document.getElementsByName(<em>name</em>) 
</pre>
<ul>
  <li><code>elements</code> is an {{domxref("HTMLCollection")}} of elements.</li>
  <li><code>name</code> is the value of the <code>name</code> attribute of the element.</li>
</ul>
<h2 id="Example" name="Example">Example</h2>
<pre class="brush:html">
&lt;!DOCTYPE html&gt;
&lt;html lang="en"&gt;
&lt;head&gt;
 ...
&lt;/head&gt;

&lt;body&gt;
&lt;form name="up"&gt;&lt;input type="text"&gt;&lt;/form&gt;
&lt;form name="down"&gt;&lt;input type="text"&gt;&lt;/form&gt;

&lt;script&gt;
var up_forms = document.getElementsByName("up");
w(up_forms[0].tagName); // returns "FORM"
&lt;/script&gt;
&lt;/body&gt;
&lt;/html&gt;
</pre>
<h2 id="Notes" name="Notes">Notes</h2>
<p>The <a href="/en-US/docs/DOM/element.name"><code>name</code></a> attribute is only applicable to (X)HTML documents. The method returns all elements with a name attribute, such as {{htmlelement("meta")}} or {{htmlelement("object")}} or even if <code>name</code> is placed on elements which do not support a name attribute at all.</p>
<p><code>document.getElementsByName()</code> returns an {{domxref("HTMLCollection")}} of all the elements with a given value for the name attribute.</p>
<h2 id="Specifications" name="Specifications">Specifications</h2>
<ul>
  <li><a href="http://www.w3.org/TR/DOM-Level-2-HTML/html.html#ID-71555259">DOM Level 2 HTML: getElementsByName</a></li>
  <li><a href="http://www.whatwg.org/html/#dom-document-getelementsbyname" title="http://www.whatwg.org/html/#dom-document-getelementsbyname">HTML5: getElementsByName</a></li>
</ul>
<!--languages( { "fr": "fr/DOM/document.getElementsByName", "pl": "pl/DOM/document.getElementsByName" } )-->
Revert to this revision