PHP & JavaScript Room :: 設置サンプル

実行結果

ソース

<form action="#">
    <select id="pulldown">
        <option value="" selected>---------</option>
    </select>
    <input type="button" value="プルダウン生成" onclick="fCreatePulldownMenuA()" />
</form>

<script type="text/javascript">
function fCreatePulldownMenuA(){
    var todofuken=["---------","北海道","本州","四国","九州","沖縄"];
    var selObj=document.getElementById("pulldown");
    var length=selObj.childNodes.length-1;
    if(length>0){
        /* ノード削除:プルダウン初期化 */
        for(var i=length; i>0; i--){
            var childObj=selObj.childNodes[i];
            if(childObj.nodeName!=undefined){
                selObj.removeChild(childObj);
            }
        }
    }
    /* ノード生成:OPTION要素生成 */
    for(var i in todofuken){
        var optObj=document.createElement("option");
        if(navigator.userAgent.indexOf("MSIE")>-1){
            optObj.innerText=todofuken[i];
        }else{
            optObj.textContent=todofuken[i];
        }
        /* ノード追加 */
        selObj.appendChild(optObj);
    }
}
</script>

polarized women