Document.getElementsByName()

  • Revision slug: DOM/document.getElementsByName
  • Revision title: document.getElementsByName
  • Revision id: 68459
  • Created:
  • Creator: Ms2ger
  • Is current revision? No
  • Comment Cleanup; Validity of the name attribute is already discussed in element.name; 26 words added, 332 words removed

Revision Content

{{ DomRef() }}

Summary

Returns a list of elements with a given name in the HTML document.

Syntax

elements = document.getElementsByName(name) 
  • elements is a NodeList of elements.
  • name is the value of the name attribute of the element.

Example

<!DOCTYPE html>
<form name="up"><input type="text"></form>
<form name="down"><input type="text"></form> 
<script>
up_forms = document.getElementsByName("up"); 
w(up_forms[0].tagName); // returns "FORM"
</script>

Notes

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

document.getElementsByName returns a NodeList of all the elements with a given value for the name attribute.

Specification

{{ languages( { "fr": "fr/DOM/document.getElementsByName", "ja": "ja/DOM/document.getElementsByName", "pl": "pl/DOM/document.getElementsByName" } ) }}

Revision Source

<p>{{ DomRef() }}</p>
<h3 name="Summary">Summary</h3>
<p>Returns a list of elements with a given <code><a href="/en/DOM/element.name" title="en/DOM/element.name">name</a></code> in the HTML document.</p>
<h3 name="Syntax">Syntax</h3>
<pre class="eval"><em>elements</em> = document.getElementsByName(<em>name</em>) 
</pre>
<ul> <li><code>elements</code> is a <a href="/en/NodeList" title="en/NodeList">NodeList</a> of elements.</li> <li><code>name</code> is the value of the <code>name</code> attribute of the element.</li>
</ul>
<h3 name="Example">Example</h3>
<pre class="brush: html">&lt;!DOCTYPE html&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;
up_forms = document.getElementsByName("up"); 
w(up_forms[0].tagName); // returns "FORM"
&lt;/script&gt;</pre>
<h3 name="Notes">Notes</h3>
<p>The <a href="/en/DOM/element.name" title="en/DOM/element.name">name</a> attribute is only applicable to (X)HTML documents. The method returns all elements with a name attribute, such as &lt;meta&gt; or &lt;object&gt; or even if 'name' is placed on elements which do not support a name attribute at all.</p>
<p><code>document.getElementsByName</code> returns a <a href="/En/DOM/NodeList" title="En/DOM/NodeList">NodeList</a> of all the elements with a given value for the name attribute.</p>
<h3>Specification</h3>
<ul> <li><a class="external" href="http://www.w3.org/TR/DOM-Level-2-HTML/html.html#ID-71555259">DOM Level 2 HTML: getElementsByName</a></li> <li><a class=" external" href="http://www.whatwg.org/html/#dom-document-getelementsbyname" title="http://www.whatwg.org/html/#dom-document-getelementsbyname">HTML5: getElementsByName</a></li>
</ul>
<p>{{ languages( { "fr": "fr/DOM/document.getElementsByName", "ja": "ja/DOM/document.getElementsByName", "pl": "pl/DOM/document.getElementsByName" } ) }}</p>
Revert to this revision