2

Want to populate dynamically combobox-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, combobox, 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="combo" id="combo"></select>
        </fieldset>
    </BODY>
</HTML>

My Javascript:-

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

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

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

1
  • What is not working? Is there a specific problem here? Commented Feb 20, 2012 at 7:23

2 Answers 2

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 combo = document.getElementById("combo");

    var option = document.createElement("option");
    option.text = textb.value;
    option.value = textb.value;
    try {
        combo.add(option, null); //Standard
    }catch(error) {
        combo.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 ()

Sign up to request clarification or add additional context in comments.

Comments

2

in your javascript code change if to try

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.