わびさびサンプルソース

WindowsやHTML5などのプログラムのサンプルコードやフリーソフトを提供します。

<select>タグの中の<option>タグの数を取得する

<option>タグの数を取得

<select>タグ中の<option>タグの数は、'select'エレメントの'length'属性に格納されています。

<select>タグ中の<option>タグの数を取得する
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
<!doctype html>
<html>
<head>
<meta charset="UTF-8">
<script type="text/javascript" language="javascript">
<!--
function OnLoad()
{
    // selectエレメント取得
    var elmSelect = document.getElementById( "id_select" );
 
    // optionタグの数を取得
    var length = elmSelect.length;
 
    // lengthを表示
    document.getElementById( "id_result" ).innerHTML = "select.length = " + length + "<br/>";
}
// -->
</script>
</head>
<body onload="OnLoad()">
<h1>&lt;select&gt;タグの中の&lt;option&gt;タグの数を取得する</h1>
<select id="id_select">
<option>アイテム1</option>
<option>アイテム2</option>
<option>アイテム3</option>
<option>アイテム4</option>
<option>アイテム5</option>
</select>
<h2>実行結果</h2>
<div id="id_result">
</div>
</body>
</html>

実行結果

select.length = 5






わびさびサンプルソース

WindowsやHTML5などのプログラムのサンプルコードやフリーソフトを提供します。