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
Fundamentals
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.
Named — document.forms.login or document.forms["login"].
Controls — use form.elements for inputs inside a form (MDN).
Foundation
📝 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);
}
Compare
⚖️ How to reach a form (safe vs unsafe)
Approach
Example
Use?
document.forms index
document.forms[0]
Yes
document.forms name
document.forms.login
Yes (MDN)
getElementById
document.getElementById("login")
Yes
querySelector
document.querySelector("form#login")
Yes
document["login"]
Named shortcut on Document
No — discouraged (MDN)
Inside a form
📄 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.
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.
WidelyAvailable
Google ChromeFull support · Desktop & Mobile
Full support
Mozilla FirefoxFull support · Desktop & Mobile
Full support
Apple SafariFull support · macOS & iOS
Full support
Microsoft EdgeFull support · Chromium
Full support
OperaFull support · Modern versions
Full support
Internet ExplorerSupported in legacy IE
Full support
Document.formsBaseline 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].
Wrap Up
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.
Forget that the collection is live when caching length in loops
Summary
Key Takeaways
Knowledge Unlocked
Five things to remember about document.forms
Live HTMLCollection of every <form> — index, name, elements.
5
Core concepts
📄01
Returns
HTMLCollection
API
✓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"].