aboutsummaryrefslogtreecommitdiff
path: root/files/ru/web/api/htmlformelement/elements/index.html
blob: a1e9d8d068f53db9c7ac81b98a89fde71be09c4d (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
91
92
93
94
95
96
97
98
99
100
---
title: HTMLFormElement.elements
slug: Web/API/HTMLFormElement/elements
translation_of: Web/API/HTMLFormElement/elements
---
<div>{{APIRef("HTML DOM")}}</div>

<p><span class="seoSummary">The {{domxref("HTMLFormElement")}} property <code><strong>elements</strong></code> returns an {{domxref("HTMLFormControlsCollection")}} listing all the form controls contained in the {{HTMLElement("form")}} element.</span> Independently, you can obtain just the number of form controls using the {{domxref("HTMLFormElement.length", "length")}} property.</p>

<p>You can access a particular form control in the returned collection by using either an index or the element's {{domxref("Element.name", "name")}} or {{domxref("Element.id", "id")}}.</p>

<p>Prior to HTML 5, the returned object was an {{domxref("HTMLCollection")}}, on which <code>HTMLFormControlsCollection</code> is based.</p>

<div class="note">
<p><strong>Note:</strong> Similarly, you can get a list of all of the forms contained within a given document using the document's {{domxref("Document.forms", "forms")}} property.</p>
</div>

<h2 id="Syntax">Syntax</h2>

<pre class="syntaxbox"><em>nodeList</em> = <em>HTMLFormElement</em>.elements
</pre>

<h3 id="Value">Value</h3>

<p>An {{domxref("HTMLFormControlsCollection")}} containing all non-image controls in the form. This is a live collection; if form controls are added to or removed from the form, this collection will update to reflect the change.</p>

<p>The form controls in the returned collection are in the same order in which they appear in the form by following a preorder, depth-first traversal of the tree. This is called <strong>tree order</strong>.</p>

<p>{{page("/en-US/docs/Web/API/HTMLFormElement", "Elements that are considered form controls")}}</p>

<h2 id="Example">Example</h2>

<h3 id="Quick_syntax_example">Quick syntax example</h3>

<p>In this example, we see how to obtain the list of form controls as well as how to access its members by index and by name or ID.</p>

<pre class="brush: html">&lt;form id="my-form"&gt;
  &lt;input type="text" name="username"&gt;
  &lt;input type="text" name="full-name"&gt;
  &lt;input type="password" name="password"&gt;
&lt;/form&gt;</pre>

<pre class="brush:js">var inputs = document.getElementById("my-form").elements;
var inputByIndex = inputs[0];
var inputByName = inputs["username"];
</pre>

<h3 id="Accessing_form_controls">Accessing form controls</h3>

<p>This example gets the form's element list, then iterates over the list, looking for {{HTMLElement("input")}} elements of type <code><a href="/en-US/docs/Web/HTML/Element/input/text">"text"</a></code> so that some form of processing can be performed on them.</p>

<pre class="brush: js">var inputs = document.getElementById("my-form").elements;

// Iterate over the form controls
for (i = 0; i &lt; inputs.length; i++) {
  if (inputs[i].nodeName === "INPUT" &amp;&amp; inputs[i].type === "text") {
    // Update text input
    inputs[i].value.toLocaleUpperCase();
  }
}
</pre>

<h3 id="Disabling_form_controls">Disabling form controls</h3>

<pre class="brush: js">var inputs = document.getElementById("my-form").elements;

// Iterate over the form controls
for (i = 0; i &lt; inputs.length; i++) {
  // Disable all form controls
  inputs[i].setAttribute("disabled", "");
}
</pre>

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

<table class="standard-table">
 <tbody>
  <tr>
   <th scope="col">Specification</th>
   <th scope="col">Status</th>
   <th scope="col">Comment</th>
  </tr>
  <tr>
   <td>{{SpecName('HTML WHATWG', '#dom-form-elements', 'HTMLFormElement.elements')}}</td>
   <td>{{ Spec2('HTML WHATWG') }}</td>
   <td></td>
  </tr>
  <tr>
   <td>{{SpecName("DOM2 HTML", "html.html#ID-76728479", "HTMLFormElement.elements")}}</td>
   <td>{{Spec2("DOM2 HTML")}}</td>
   <td>Initial definition</td>
  </tr>
 </tbody>
</table>

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



<p>{{Compat("api.HTMLFormElement.elements")}}</p>