Please note, this is a STATIC archive of website developer.mozilla.org from 03 Nov 2016, cach3.com does not collect or store any user information, there is no "phishing" involved.

Revision 822597 of Document.forms

  • Revision slug: Web/API/Document/forms
  • Revision title: Document.forms
  • Revision id: 822597
  • Created:
  • Creator: Delapouite
  • Is current revision? No
  • Comment added link to <form>

Revision Content

{{APIRef("DOM")}}

forms returns a collection (an {{domxref("HTMLCollection")}}) of the {{HTMLElement("form")}} elements within the current document.

Syntax

collection = document.forms;

Example: Getting form information

<!DOCTYPE html>
<html lang="en">

<head>
<title>document.forms example</title>
</head>

<body>

<form id="robby">
  <input type="button" onclick="alert(document.forms[0].id);" value="robby's form" />
</form>

<form id="dave">
  <input type="button" onclick="alert(document.forms[1].id);" value="dave's form" />
</form>

<form id="paul">
  <input type="button" onclick="alert(document.forms[2].id);" value="paul's form" />
</form>

</body>
</html>

Example 2:Getting an element from within a form

var selectForm = document.forms[index];
var selectFormElement = document.forms[index].elements[index];

Specification

See also

Revision Source

<div>{{APIRef("DOM")}}</div>

<p><code>forms</code> returns a collection (an {{domxref("HTMLCollection")}}) of the {{HTMLElement("form")}} elements within the current document.</p>

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

<pre class="syntaxbox">
<var>collection</var> = document.forms;</pre>

<h2 id="Example" name="Example">Example: Getting form information</h2>

<pre class="brush:html">
&lt;!DOCTYPE html&gt;
&lt;html lang="en"&gt;

&lt;head&gt;
&lt;title&gt;document.forms example&lt;/title&gt;
&lt;/head&gt;

&lt;body&gt;

&lt;form id="robby"&gt;
  &lt;input type="button" onclick="alert(document.forms[0].id);" value="robby's form" /&gt;
&lt;/form&gt;

&lt;form id="dave"&gt;
  &lt;input type="button" onclick="alert(document.forms[1].id);" value="dave's form" /&gt;
&lt;/form&gt;

&lt;form id="paul"&gt;
  &lt;input type="button" onclick="alert(document.forms[2].id);" value="paul's form" /&gt;
&lt;/form&gt;

&lt;/body&gt;
&lt;/html&gt;
</pre>

<h2 id="Example_2Getting_an_element_from_within_a_form">Example 2:Getting an element from within a form</h2>

<pre class="brush: js">
var selectForm = document.forms[index];
var selectFormElement = document.forms[index].elements[index];
</pre>

<h2 id="Specification" name="Specification">Specification</h2>

<ul>
 <li><a class="external" href="https://www.w3.org/TR/DOM-Level-2-HTML/html.html#ID-1689064">forms DOM Level 2 HTML: forms</a></li>
</ul>

<h2 id="See_Also" name="See_Also">See also</h2>

<ul>
 <li><a href="/en-US/docs/DOM/HTMLFormElement">DOM:form</a></li>
</ul>
Revert to this revision