HTML DOMドキュメントgetElementsByName()
例
「fname」という名前のすべての要素を取得します。
let collection = document.getElementsByName("fname");
以下の「自分で試してみてください」の例をもっと見てください。
定義と使用法
このgetElementsByName()
メソッドは、指定された名前の要素のコレクションを返します。
このgetElementsByName()
メソッドはHTMLCollectionを返します。
構文
document.getElementsByName(name)
パラメーター
Parameter | Description |
name | Required. The name value of the elements. |
戻り値
タイプ | 説明 |
物体 | HTMLCollectionオブジェクト。 指定された名前の要素のコレクション。 要素は、ドキュメントに表示されるとおりに並べ替えられます。 |
その他の例
name = "animal"の要素の数:
let num = document.getElementsByName("animal").length;
「animal」という名前のtype = "checkbox"を持つすべての<input>要素をチェックします。
const collection = document.getElementsByName("animal");
for (let i = 0; i < collection.length; i++) {
if (collection[i].type == "checkbox") {
collection[i].checked = true;
}
}
ブラウザのサポート
document.getElementsByName()
DOMレベル1(1998)の機能です。
すべてのブラウザで完全にサポートされています。
Chrome | IE | Edge | Firefox | Safari | Opera |
Yes | 9-11 | Yes | Yes | Yes | Yes |