Dynamic combobox-listbox-drop-down using javascript
Want to populate dynamically combobox-listbox-drop-down using javascript? Let us see a very simple script to do this. First let us see createElement() of document object in javascript.| 1 2 3 4 5 6 7 8 | //Create a table element dynamically vartable = document.createElement("table");  //Create a select element dynamically varselect = document.createElement("select");  //Create a option element dynamically varoption = document.createElement("option"); | 
Let us see how can we populate a dropdown or combobox using this method. Following is the html file of our example:
| 01 02 03 04 05 06 07 08 09 10 11 12 13 14 15 16 | <HTML>     <HEAD>         <TITLE>Dynamically populating drop down, combobox, list box using JavaScript</TITLE>         <SCRIPTlanguage="javascript"src="config.js"></SCRIPT>     </HEAD>     <BODYstyle="font-family: sans-serif">          <fieldset>             <legend>Combo box</legend>             Add to Combo: <inputtype="text"name="txtCombo"id="txtCombo"/>             <inputtype="button"value="Add"onclick="addCombo()">             <br/>             Combobox: <selectname="combo"id="combo"></select>         </fieldset>     </BODY> </HTML> | 
| 01 02 03 04 05 06 07 08 09 10 11 12 13 14 | functionaddCombo() {     vartextb = document.getElementById("txtCombo");     varcombo = document.getElementById("combo");      varoption = 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 = ""; } | 
 
No comments:
Post a Comment