【Javascript】動的に生成したoption要素のtextが反映されない(IEで)
IEはJavascriptで動的に生成したselect(セレクトボックス、プルダウン)のoptionのtextを表示しない。
あるselect要素にoption要素を追加して、option.textに文字列を登録すると、プルダウンはその分長くなりますが、表示されて欲しい文字が表示されません。
IEでは innerText 属性を使うらしいです。
お試し用ソースです。
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>セレクトボックステスト</title>
<script type="text/javascript">
window.onload = function() {
var select = document.getElementById('sample')
var option = document.createElement('option')
//valueはvalueに書き込み
option.value = '123456'
//textはinnerTextが使えるかどうかで場合わけ
//IEで試したときとIE以外で試したときで結果が変わります
if (typeof(option.innerText) != 'undefined') {
option.innerText = '表示用文字列 IEの場合'
} else {
option.text = '表示用文字列 通常の場合'
}
//プルダウンに追加
select.appendChild(option)
}
</script>
</head>
<body>
<select id="sample"></select>
</body>
</html>

同じこと困っていたのですが。、解決しました。ありがとうございます。
お役に立てて何よりです。コメントありがとうございます。 ^-^
【Javascript】動的に生成したoption要素のtextが反映されない(IEで) その2 at softelメモ 2010年3月13日 11:59
[...] option.text を使った場合の困りごとを書いたことがありますが、DOM関数版も書いておきます。 [...]