aboutsummaryrefslogtreecommitdiff
path: root/files/ru/web/api/document/getelementsbyname/index.html
blob: 73b8921d61ee2b29a19a3d5904872573d8dd0329 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
---
title: Document.getElementsByName()
slug: Web/API/Document/getElementsByName
translation_of: Web/API/Document/getElementsByName
---
<div>{{APIRef("DOM")}}</div>

<p><span class="seoSummary">Метод <strong><code>getElementsByName()</code></strong>  объекта {{domxref("Document")}} возвращает коллекцию {{domxref("NodeList")}} элементов с заданным  {{domxref("element.name","name")}}.</span></p>

<h2 id="Синтаксис">Синтаксис</h2>

<pre class="syntaxbox">var <var>elements</var> = document.getElementsByName(<var>name</var>);
</pre>

<ul>
 <li><var>elements</var> — это живая {{domxref("NodeList")}} коллекция. То есть,  она автоматически обновляется, когда элементы с таким же <code>name</code> добавляются/удаляются из документа.</li>
 <li><var>name </var>— это значение поля  <code>name</code> элемента(элементов).</li>
</ul>

<h2 id="Пример">Пример</h2>

<pre class="brush:html">&lt;!DOCTYPE html&gt;
&lt;html lang="en"&gt;
&lt;title&gt;Example: using document.getElementsByName&lt;/title&gt;

&lt;input type="hidden" name="up"&gt;
&lt;input type="hidden" name="down"&gt;

&lt;script&gt;
  var up_names = document.getElementsByName("up");
  console.log(up_names[0].tagName); // displays "INPUT"
&lt;/script&gt;
&lt;/html&gt;
</pre>

<h2 id="Notes">Notes</h2>

<p>The {{domxref("element.name","name")}} attribute can only be applied in (X)HTML documents.</p>

<p>The returned {{domxref("NodeList")}} Collection contains <em>all</em> elements with the given <code>name</code>, such as {{htmlelement("meta")}}, {{htmlelement("object")}}, and even elements which do not support the <code>name</code> attribute at all.</p>

<div class="warning">
<p>The <strong>getElementsByName</strong> method works differently in IE10 and below. There, <code>getElementsByName()</code> also returns elements that have an <a href="/en-US/docs/Web/HTML/Global_attributes/id"><code>id</code> attribute</a> with the specified value. Be careful not to use the same string as both a <code>name</code> and an <code>id</code>.</p>
</div>

<div class="warning">
<p>The <strong>getElementsByName</strong> method works differently in IE. There, <code>getElementsByName()</code> does not return all elements which may not have a <code>name</code> attribute (such as <code>&lt;span&gt;</code>).</p>
</div>

<div class="warning">
<p>Both IE and Edge return an {{domxref("HTMLCollection")}}, not a {{domxref("NodeList")}}</p>
</div>

<h2 id="Specifications">Specifications</h2>

<table class="standard-table">
 <thead>
  <tr>
   <th scope="col">Specification</th>
   <th scope="col">Status</th>
   <th scope="col">Comment</th>
  </tr>
 </thead>
 <tbody>
  <tr>
   <td>{{SpecName('HTML WHATWG', '#dom-document-getelementsbyname', "Document.getElementsByName()")}}</td>
   <td>{{ Spec2('HTML WHATWG') }}</td>
   <td> </td>
  </tr>
  <tr>
   <td>{{SpecName("DOM2 HTML", "html.html#ID-71555259", "Document.getElementsByName()")}}</td>
   <td>{{Spec2("DOM2 HTML")}}</td>
   <td>Initial definition</td>
  </tr>
 </tbody>
</table>

<h2 id="Browser_compatibility">Browser compatibility</h2>



<p>{{Compat("api.Document.getElementsByName")}}</p>

<h2 id="See_also">See also</h2>

<ul>
 <li>{{domxref("document.getElementById()")}} to return a reference to an element by its unique <code>id</code></li>
 <li>{{domxref("document.getElementsByTagName()")}} to return references to elements with the same <a href="/en-US/docs/Web/API/Element/tagName">tag name</a></li>
 <li>{{domxref("document.querySelector()")}} to return references to elements via CSS selectors like <code>'div.myclass'</code></li>
</ul>