最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

Dynamic combobox-listbox-drop-down using javascript - Stack Overflow

programmeradmin2浏览0评论

Want to populate dynamically bobox-listbox-drop-down using javascript.

var table = document.createElement("table");
var select = document.createElement("select");
var option = document.createElement("option");

My HTML Code:-

<HTML>
    <HEAD>
        <TITLE>Dynamically populating drop down, bobox, list box using JavaScript</TITLE>
        <SCRIPT language="javascript" src="config.js"></SCRIPT>
    </HEAD>
    <BODY style="font-family: sans-serif">

        <fieldset>
            <legend>Combo box</legend>
            Add to Combo: <input type="text" name="txtCombo" id="txtCombo"/>
            <input type="button" value="Add" onclick="addCombo()">
            <br/>
            Combobox: <select name="bo" id="bo"></select>
        </fieldset>
    </BODY>
</HTML>

My Javascript:-

function addCombo(a, b) {
    var textb = document.getElementById("txtCombo");
    var bo = document.getElementById("bo");

    var option = document.createElement("option");
    option.text = textb.value;
    option.value = textb.value;
    if {
        bo.add(option, null); //Standard
    }catch(error) {
        bo.add(option); // IE only
    }
    textb.value = "";
}

Now still its not working is there any issue in the code? Do I am missing something?

Want to populate dynamically bobox-listbox-drop-down using javascript.

var table = document.createElement("table");
var select = document.createElement("select");
var option = document.createElement("option");

My HTML Code:-

<HTML>
    <HEAD>
        <TITLE>Dynamically populating drop down, bobox, list box using JavaScript</TITLE>
        <SCRIPT language="javascript" src="config.js"></SCRIPT>
    </HEAD>
    <BODY style="font-family: sans-serif">

        <fieldset>
            <legend>Combo box</legend>
            Add to Combo: <input type="text" name="txtCombo" id="txtCombo"/>
            <input type="button" value="Add" onclick="addCombo()">
            <br/>
            Combobox: <select name="bo" id="bo"></select>
        </fieldset>
    </BODY>
</HTML>

My Javascript:-

function addCombo(a, b) {
    var textb = document.getElementById("txtCombo");
    var bo = document.getElementById("bo");

    var option = document.createElement("option");
    option.text = textb.value;
    option.value = textb.value;
    if {
        bo.add(option, null); //Standard
    }catch(error) {
        bo.add(option); // IE only
    }
    textb.value = "";
}

Now still its not working is there any issue in the code? Do I am missing something?

Share Improve this question asked Feb 20, 2012 at 7:21 user1201452user1201452 1
  • What is not working? Is there a specific problem here? – gideon Commented Feb 20, 2012 at 7:23
Add a ment  | 

2 Answers 2

Reset to default 2

You need to correct your function to get this thing done, there is an issue in syntex:-

Change your Javascript to this:-

function addCombo() {
    var textb = document.getElementById("txtCombo");
    var bo = document.getElementById("bo");

    var option = document.createElement("option");
    option.text = textb.value;
    option.value = textb.value;
    try {
        bo.add(option, null); //Standard
    }catch(error) {
        bo.add(option); // IE only
    }
    textb.value = "";
}

With catch you need to use try And you don't need to to put anything in the function ()

in your javascript code change if to try

发布评论

评论列表(0)

  1. 暂无评论