JavaScript Document forms Property

Beginner
5 min read
Updated: Nov 2026
3 examples
Property

Definition

document.forms is a read-only property that returns a live HTMLCollection of every <form> in the document. Each item is an HTMLFormElement (MDN).

Read the notes below before you get or set this property. Prefer feature detection when support is limited.

Syntax

Use document.forms. Start with this line.

JavaScript
document.forms
CallWhat it does
document.formsPrimary signature.

Built into the browser. Read or assign document.forms — no import required.

Library declarations
JavaScript
document.forms

Parameters

ParameterDescription
Writable?Optional. Read-only

Return Value

Return ValueDescription
*Live HTMLCollection

JavaScript Examples

Example 1 — Count with forms.length

Count every <form> on the page.

JavaScript
const count = document.forms.length;
console.log("form count:", count);

if (count === 0) {
  console.log("No <form> elements on this page");
}
Try It Yourself

How It Works

  1. Read document.forms. Live HTMLCollection of forms (MDN).
  2. Use .length. Number of <form> tags only.
  3. Empty pages. Length is 0 when there are no forms.
  4. Guard before indexing. Check count before forms[0].

Example 2 — Named Form and elements

MDN: access document.forms.login, then set control placeholders.

JavaScript
const loginForm = document.forms.login;
loginForm.elements.email.placeholder = "[email protected]";
loginForm.elements.password.placeholder = "password";

console.log(loginForm.name);
console.log(loginForm.elements.email.placeholder);
Try It Yourself

How It Works

  1. Look up by name. document.forms.login matches name="login".
  2. Use elements. Collection of the form’s controls (MDN).
  3. Named controls. elements.email is the input with that name.
  4. Stay on document.forms. Do not use document["login"].

Example 3 — Live Collection Updates

Appending a form increases document.forms.length automatically.

JavaScript
const before = document.forms.length;

const form = document.createElement("form");
form.name = "extra";
document.body.appendChild(form);

console.log(before, "→", document.forms.length);
console.log(document.forms.extra.name);
Try It Yourself

How It Works

  1. Store length before the change.
  2. Create and append a form. DOM update is enough.
  3. Read length again. Live collection increases by one.
  4. Named access works immediately for the new form’s name.

Browser Support

Document.forms is marked Baseline Widely available on MDN (since June 2018). Logos use the shared browser-image-sprite.png sprite from this project.

✓ Baseline · Widely available

Document.forms

Read-only live HTMLCollection of every form element in the document.

Widely Available
Google Chrome Full support · Desktop & Mobile
Full support
Mozilla Firefox Full support · Desktop & Mobile
Full support
Apple Safari Full support · macOS & iOS
Full support
Microsoft Edge Full support · Chromium
Full support
Opera Full support · Modern versions
Full support
Internet Explorer Supported in legacy IE
Full support
Document.forms Baseline support

Bottom line: Use document.forms to list and look up forms. Prefer document.forms.name over document["name"]. See the MDN Document.forms reference.

Frequently Asked Questions

A read-only HTMLCollection of every <form> element in the document. Each item is an HTMLFormElement. If there are no forms, length is zero (MDN).
No. MDN marks Document.forms as Baseline Widely available (since June 2018). It is a standard Document collection property.
Use document.forms.login or document.forms["login"] for a form with name="login". Prefer this over document["login"].
MDN warns that pattern is dangerous and discouraged — it can conflict with existing or future Document APIs. Always use document.forms for named forms.
Use the form's elements collection: document.forms[0].elements or loginForm.elements.email (MDN).
Yes. HTMLCollection updates when forms are added or removed from the document.

Next: Document fragmentDirective

Learn the Document fragmentDirective property for text fragments.

fragmentDirective tutorial →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

11 people found this page helpful