JavaScript Document forms Property

Beginner
⏱️ 11 min read
📚 Updated: Jul 2026
🎯 5 Examples
🚀 5 Try-it labs
Baseline Widely available
Instance property

What You’ll Learn

Document.forms is a read-only instance property that returns a live HTMLCollection of every <form> in the document. Learn index and named access, form.elements, why document["name"] is unsafe, and five examples with try-it labs.

01

Kind

Read-only property

02

Returns

HTMLCollection

03

Items

HTMLFormElement

04

Access

Index or name

05

Controls

form.elements

06

Status

Baseline widely

Introduction

Almost every website has at least one form—login, search, checkout, contact. When a script needs every form on the page (or one form by name), document.forms is the built-in collection to use.

MDN: the forms read-only property returns an HTMLCollection listing all the <form> elements in the document. Each item is an HTMLFormElement.

⚠️
Prefer document.forms over document["name"]

MDN: accessing forms as document["login-form"] is dangerous and discouraged—it can clash with browser APIs. Always use document.forms["login-form"] (or document.forms.login) instead.

Related Document tutorials: fonts, embeds, Document constructor.

Understanding Document.forms

A read-only instance property on Document. Its value is a live HTMLCollection of every <form> element (MDN).

  • ValueHTMLCollection of HTMLFormElement items.
  • Empty safe — no forms → length 0 (MDN).
  • Live — updates when forms are added or removed.
  • Indexeddocument.forms[0], document.forms.item(0).
  • Nameddocument.forms.login or document.forms["login"].
  • Controls — use form.elements for inputs inside a form (MDN).

📝 Syntax

JavaScript
document.forms

Value

An HTMLCollection of all document forms. Each item is an HTMLFormElement (MDN).

Common patterns

JavaScript
const count = document.forms.length;
const first = document.forms[0];
const login = document.forms.login; // or document.forms["login"]

for (const form of document.forms) {
  console.log(form.id || form.name);
}

📄 HTMLFormElement.elements

MDN: you can access a form’s component user input elements with HTMLFormElement.elements—another HTMLCollection / HTMLFormControlsCollection of controls belonging to that form.

JavaScript
const loginForm = document.forms.login;
loginForm.elements.email.placeholder = "test@example.com";
loginForm.elements.password.placeholder = "password";

⚡ Quick Reference

GoalCode / note
All formsdocument.forms
Countdocument.forms.length
First formdocument.forms[0]
By namedocument.forms.login
Field inside formdocument.forms.login.elements.email
Avoiddocument["login"] (MDN warning)
MDN statusBaseline Widely available

🔍 At a Glance

Four facts about document.forms.

Type
HTMLCollection

Read-only, live

Contains
<form>

HTMLFormElement

Named
forms.name

Safe access

Status
baseline

Standard API

📋 id vs name on forms

AttributeBest forAccess tip
idCSS / getElementByIdUnique in the document
namedocument.forms.nameNamed lookup on the collection (MDN)
BothClarity in large appsKeep them equal when possible
NeitherAnonymous formsUse index or querySelector

Examples Gallery

Examples follow MDN Document: forms. Use View Output or Try It Yourself for each case.

📚 Getting Started

Count forms and read them by index (MDN-style).

Example 1 — Count Forms with document.forms.length

Empty documents return a collection with length zero (MDN).

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

Only <form> tags are counted—not loose inputs outside a form.

Example 2 — MDN: Get Form Info by Index

Click handlers log each form’s id via document.forms[i].

JavaScript
document.querySelectorAll("input[type=button]").forEach((button, i) => {
  button.addEventListener("click", () => {
    console.log(document.forms[i].id);
  });
});
Try It Yourself

How It Works

Index order follows document order of <form> elements.

📈 Named Access, Elements & Live Updates

Look up by name, reach controls, prove the collection is live.

Example 3 — MDN: Named Form Access

Use document.forms.login (or bracket notation) for a named form.

JavaScript
const loginForm = document.forms.login; // Or document.forms["login"]
loginForm.elements.email.placeholder = "test@example.com";
loginForm.elements.password.placeholder = "password";
console.log("form name:", loginForm.name);
Try It Yourself

How It Works

Do not write document.login / document["login"] for new code (MDN warning).

Example 4 — MDN: Element from Within a Form

Walk from the forms collection into elements.

JavaScript
const selectForm = document.forms[0];
const firstControl = selectForm.elements[0];
console.log(firstControl.tagName, firstControl.name || firstControl.type);

// Named controls are often clearer:
console.log(selectForm.elements.email.value);
Try It Yourself

How It Works

elements includes successful form controls associated with that form.

Example 5 — Live Collection Updates Automatically

Appending a <form> increases document.forms.length.

JavaScript
const before = document.forms.length;

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

const after = document.forms.length;
console.log("before:", before, "after:", after);
console.log("named:", document.forms.extra.name);
Try It Yourself

How It Works

A static querySelectorAll("form") snapshot would not grow unless you call it again.

🚀 Common Use Cases

  • Inventory forms — count or list every form for debugging.
  • Login / checkout scripts — grab document.forms.login by name.
  • Fill helpers — set placeholders or values via form.elements.
  • Multi-step wizards — switch among several forms by index.
  • Avoid name clashes — never rely on document[formName] (MDN).
  • Validation loops — iterate document.forms and check each checkValidity().

🧠 How document.forms Works

1

HTML contains <form> tags

Authors place one or more forms in the document tree.

Markup
2

Browser builds a filtered collection

HTML: an HTMLCollection of form elements rooted at the Document.

Filter
3

You read by index or name

forms[0], forms.login, then dig into elements.

Access
4

DOM changes stay in sync

Add or remove forms and the same collection reflects the new set.

📝 Notes

  • MDN: Baseline Widely available (since June 2018) — no Deprecated / Experimental / Non-standard banner.
  • Value is an HTMLCollection of HTMLFormElement items.
  • Always prefer document.forms over document["form-name"] (MDN warning).
  • Use HTMLFormElement.elements for controls inside a form (MDN).
  • Related: fonts, embeds, Document constructor.

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> — index, name, and form.elements.

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 safely. Prefer named access on forms, then form.elements for controls — never document[formName].

Conclusion

Document.forms is the standard, live list of every <form> in a document. Use index or named access on document.forms, dig into elements for controls, and avoid the unsafe document[name] shortcut.

Continue with fragmentDirective, fonts, embeds, or the JavaScript hub.

💡 Best Practices

✅ Do

  • Use document.forms for a live list of all forms
  • Prefer named access: document.forms.login
  • Reach controls with form.elements
  • Give important forms a clear name or id
  • Check length before indexing [0]

❌ Don’t

  • Use document["form-name"] (MDN: discouraged)
  • Assume forms exist without checking length
  • Confuse form id with control name
  • Assign to document.forms (read-only)
  • Forget that the collection is live when caching length in loops

Key Takeaways

Knowledge Unlocked

Five things to remember about document.forms

Live HTMLCollection of every <form> — index, name, elements.

5
Core concepts
02

Status

baseline

Standard
📝03

Items

HTMLFormElement

Forms
👤04

Named

forms.login

Safe
⚠️05

Avoid

document[name]

MDN

❓ 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.
Did you know?

Named forms have been reachable as properties of document since early browsers—that convenience is exactly why MDN warns against it today. Future Document APIs might reclaim the same property name and silently break document["login-form"].

Next: fragmentDirective

Detect text fragment support with Document.fragmentDirective.

fragmentDirective →

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.

6 people found this page helpful