Document.forms

  • Revision slug: Web/API/document.forms
  • Revision title: document.forms
  • Revision id: 409761
  • Created:
  • Creator: ethertank
  • Is current revision? No
  • Comment

Revision Content

{{DomRef()}}

Summary

forms returns a collection (an {{domxref("HTMLCollection")}}) of the 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>
  {{DomRef()}}</div>
<h2 id="Summary" name="Summary">Summary</h2>
<p><code>forms</code> returns a collection (an {{domxref("HTMLCollection")}}) of the <code>form</code> 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_2.3AGetting_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="http://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