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
2 Answers
Reset to default 2You 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