Adding options to List Box in client side Javascript

Options 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. Here is the function
function addOption(selectbox,text,value )
{var optn = document.createElement("OPTION");
optn.text = text;
optn.value = value;
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",
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>

One option selected by default

We can keep one option selected by default while adding option to list box. Here is the syntax for this .
Here i is the variable or number of particular option. To make it clear we will use this in our Listbox demo ( shown above ). We will keep the current month selected while populating the listbox. To get the current month we will use getMonth() function.

Demo of selecting default option

Here is the change in source code.

<script language="javascript">
var dt=new Date();
var dt_month=dt.getMonth() +1;
function addOption(selectbox,text,value )
	var optn = document.createElement("OPTION");
	optn.text = text;
	optn.value = value;

function addOption_list(){
var month = new Array("January","February","March","April","May","June","July","August",
"September","October","November","December"); for (var i=0; i < month.length;++i){ addOption(document.drop_list.Month_list, month[i], month[i]); if(i== dt_month){document.drop_list.Month_list.options[i].selected=true;} } } </script> </head> <body onLoad="addOption_list()";> You can see the view-> Source of this page. <br><br> <FORM name="drop_list" action="yourpage.php" method="POST" > <SELECT NAME="Month_list"> <Option value="" >Month list</option> </SELECT> </form> </body> </html>

Adding option to Datalist

Datalist is included in HTML5. The advantage of datalist is users is not limited to given options only. You can learn basics of datalist in our HTML section. By using JavaScript we can add options to datalist.

Adding options to datalist

Visitors Rating
Your Rating

More on List Box & JavaScript


"selectbox.options.add(optn);" is not working in IE...


Check the Demo link. It is working fine in IE, FireFox n Chrome


how can we give link on add more option if we want to add more text box?


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);


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


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


How can I add names to the options?
Santhosh K S


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


Excellent. Simple, with a demo. Just what I needed. Thank you!


that totally works...simple , nice and easy


in function addOption(selectbox,text,value ), can this selectbox be an id of a div for dropdown


if i am populating a dropdown list based on selection from another list, then how can i avoid duplication of options on the 2nd list, if we select the same option from the ist list again?
please reply.thank you.


hiii,am noob to php, so i need a clear example code for three linked dropdown list....
Php Expert Programmer


Thanks sir,
Worked for me first time. Saved me a lot of work.
Thanks again.

Post Comment This is for short comments only. Use the forum for more discussions.

HTML . MySQL. PHP. JavaScript. ASP. Photoshop. Articles. FORUM Contact us
©2000-2018 All rights reserved worldwide Privacy Policy Disclaimer