--- title: Document.forms slug: Web/API/Document/forms tags: - API - DOM - Document - Forms - HTML DOM - HTML forms - Proprietà - Referenza translation_of: Web/API/Document/forms ---

La proprietà di sola lettura forms dell'interfaccia {{domxref("Document")}} restituisce una {{domxref("HTMLCollection")}} che elenca tutti gli elementi {{HTMLElement("form")}} contenuti nel documento.

Note: Allo stesso modo, è possibile accedere a un elenco di elementi di input utente di un modulo utilizzando la proprietà {{domxref("HTMLFormElement.elements")}}.

Sintassi

collection = document.forms;

Valore

Un oggetto {{domxref("HTMLCollection")}} che elenca tutti i form del documento. Ogni elemento della collezione è un {{domxref("HTMLFormElement")}} che rappresenta un singolo elemento <form>.

Se il documento non ha moduli, la raccolta restituita è vuota, con una lunghezza pari a zero.

Esempi

Ottenere informazioni sul modulo

<!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>

Ottenere un elemento all'interno di un modulo

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

Accesso al modulo con nome

<!DOCTYPE html>
<html lang="en">
<head>
  <title>document.forms example</title>
</head>

<body>

<form name="login">
  <input name="email" type="email">
  <input name="password" type="password">
  <button type="submit">Log in</button>
</form>

<script>
  var loginForm = document.forms.login; // Or document.forms['login']
  loginForm.elements.email.placeholder = 'test@example.com';
  loginForm.elements.password.placeholder = 'password';
</script>
</body>
</html>

Specifiche

Specifica Stato Commento
{{SpecName('HTML WHATWG', '#dom-document-forms', 'Document.forms')}} {{ Spec2('HTML WHATWG') }}  
{{SpecName('DOM2 HTML', 'html.html#ID-1689064', 'Document.forms')}} {{ Spec2('DOM2 Events') }} Definizione iniziale.

Compatibilità con i browser

{{Compat("api.Document.forms")}}

Vedi anche

{{APIRef("DOM")}}