Document.forms
La proprietà di sola lettura forms
dell'interfaccia Document
restituisce una HTMLCollection
che elenca tutti gli elementi <form>
contenuti nel documento.
Note: Allo stesso modo, è possibile accedere a un elenco di elementi di input utente di un modulo utilizzando la proprietà HTMLFormElement.elements
.
Sintassi
collection = document.forms;
Valore
Un oggetto HTMLCollection
che elenca tutti i form del documento. Ogni elemento della collezione è un 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 |
---|---|---|
HTML Living Standard The definition of 'Document.forms' in that specification. |
Living Standard | |
Document Object Model (DOM) Level 2 HTML Specification The definition of 'Document.forms' in that specification. |
Obsolete | Definizione iniziale. |
Compatibilità con i browser
BCD tables only load in the browser
Vedi anche
- HTML forms
<form>
e l'interfacciaHTMLFormElement