HTMLDOMドキュメントフォーム
例
ドキュメント内の<form>要素の数:
let num = document.forms.length;
最初の<form>要素のIDを取得します。
let id = document.forms[0].id;
最初の<form>要素のIDを取得します。
let id = document.forms.item(0).id;
id = "myCarForm"を使用して<form>要素のHTMLコンテンツを取得します。
let html = document.forms.namedItem("myCarForm").innerHTML;
以下のその他の例。
定義と使用法
このforms
プロパティは、ドキュメント内のすべての<form>要素のコレクションを返します。
forms
プロパティはHTMLCollectionを返します。
forms
プロパティは読み取り専用です。
HTMLCollection
HTMLCollectionは、HTMLノードのコレクションです。
コレクション内のノードには、インデックス番号でアクセスできます。インデックスは0から始まります。
lengthプロパティは、コレクション内の要素の数を返します。
構文
document.forms
プロパティ
Property | Description |
length | The number of elements in the collection. |
メソッド
Method | Description |
[index] | Returns the element with the specified index (starts at 0). Returns null if the index is out of range. |
item(index) | Returns the element with the specified index (starts at 0). Returns null if the index is out of range. |
namedItem(id) | Returns the element with the specified id. Returns null if the id does not exist. |
戻り値
タイプ | 説明 |
物体 | HTMLCollectionオブジェクト。 ドキュメント内のすべての<form>要素。 ソースコードに表示されるとおりに並べ替え |
その他の例
すべての<form>要素をループして、各フォームのIDを出力します。
const forms = document.forms;
let text = "";
for (let i = 0; i < forms.length; i++) {
text += forms[i].id + "<br>";
}
form.elementsコレクションを使用して 、フォーム内の各要素の値を取得 します。
const form = document.forms[0];
let text = "";
for (let i = 0; i < form.length; i++) {
text += forms.elements[i].value + "<br>";
}
ブラウザのサポート
document.forms
DOMレベル1(1998)の機能です。
すべてのブラウザで完全にサポートされています。
Chrome | IE | Edge | Firefox | Safari | Opera |
Yes | 9-11 | Yes | Yes | Yes | Yes |