セレクトボックスに項目の追加、削除

2005/06/09


// すべてのアイテムを削除
var selectBox = document.myForm.sampleSelect;
for (var i=selectBox.options.length; i>0; i--) {
  selectBox.options[i-1] = null;
}

// 配列の内容をアイテムに追加
var items     = new Array("い", "ろ", "は", "に", "ほ", "へ", "と");
var selectBox = document.myForm.sampleSelect;
var itmIndex  = selectBox.options.length - 1;

for (var i=0; i<items.length; i++) {
  itmIndex++;
  selectBox.options[itmIndex] = new Option(items[i], itmIndex);  // new Option(label,value)
}