Adding options to List Box in client side JavascriptOptions to a drop down list box can be added dynamically using client side JavaScript. We will discuss a simple way of adding options to a list. The process of adding can be controlled based on different condition required. Depending on conditions options can be removed also.
Here we will take care that the options are added on the page load , so by default the list box is pre populated when the page is displayed. You can see the demo at the end of this page. The main function creates a new OPTION object and assigns values and text part of the option. Here is the function which collects the list box name, value and text as inputs and then adds the option to the list box.
DEMO of adding option to list box Script
Here is the function
function addOption(selectbox,text,value )
{var optn = document.createElement("OPTION");
optn.text = text;
optn.value = value;
selectbox.options.add(optn);
}
Note that each time the function is called, it adds a new option to the list box. So we can add one option by calling the function once. Like this.
addOption(document.drop_list.Month_list,”January”, “January”);
So this way we can create a drop down list box of all the months by calling the function each time for a month. So with this now you can easily create the list box. But we will add another step to this by using one array of months. So from the array of months we will loop through and add each month to the list box. Here is how to create the array
var month = new Array("January","February","March","April","May","June", "July","August","September","October","November","December");
So now once our array is ready with data, we will loop through the array by using for loop and then call the addOption function using the data of the array. Here is the code.
for (var i=0; i < month.length;++i){
addOption(document.drop_list.Month_list, month[i], month[i]);
}
You can see with this array we will able to populate the drop down list box of months. Here is the simple code for html body tag and the form with drop down list
<body onLoad="addOption_list()";>
<FORM name="drop_list" action="yourpage.php" method="POST" >
<SELECT NAME="Month_list">
<Option value="" >Month list</option>
</SELECT>
</form>
</body>
Found anything wrong or wants to improve the code by adding more features? Post your short comment here or use the Forum
|
| Suresh | 28-01-2010 |
|---|
| "selectbox.options.add(optn);" is not working in IE... | | smo | 28-01-2010 |
|---|
| Check the Demo link. It is working fine in IE, FireFox n Chrome | | Prasannjit | 08-06-2010 |
|---|
| how can we give link on add more option if we want to add more text box? | | Raza | 23-06-2010 |
|---|
For adding more items from text box:
var NewItem = document.FormName.AddMoreTextBox.value;
var optn = document.createElement("OPTION");
optn.text = NewItem;
optn.value = NewItem;
document.PlotForm.ScripName.options.add(optn);
| | rajesh | 10-07-2010 |
|---|
Please help me for my query. I have 5 text box(option A to option E) and one dropdown list. in that im showing in listitem " A to E". in my case wat i need is. if user enter values only in Textbox A and textbox B then dropdwonlist listitem should show only option A&B. same if textbox A,B,C,D has value and E textbox empty. then in dropdownlist it should not get display E.
i think it is only possible thru javascript. pls help...
Thanks in advance | | Ramesh Agye | 06-09-2010 |
|---|
Hi, sir i want when i select jan and hit submit button then open a new htm page for jan same nest...
it is possible
Ramesh Agye
| | Mikko | 06-05-2011 |
|---|
| How can I add names to the options? | | Santhosh K S | 23-04-2012 |
|---|
| i have dropdownlist having some filenames,and i have textbox, when i enter "Ab" in textbox,all file names started with Ab should list in same dropdown list useing javascript in client side, please help me | | Jim | 11-05-2012 |
|---|
| Excellent. Simple, with a demo. Just what I needed. Thank you! | | Devi | 02-01-2013 |
|---|
| that totally works...simple , nice and easy |
|