SQL PHP HTML ASP JavaScript articles and free scripts to download
JavaScript Form Validation Listbox

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.

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>

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 .

document.drop_list.Month_list.options[i].selected=true;

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.

<html>
<head>
<title></title>
<META NAME="DESCRIPTION" CONTENT="">
<META NAME="KEYWORDS" CONTENT="">

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

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>

Number of User Comments : 11


Google+

More on List Box & JavaScript
Suresh 28-01-2010
"selectbox.options.add(optn);" is not working in IE...
smo28-01-2010
Check the Demo link. It is working fine in IE, FireFox n Chrome
Prasannjit08-06-2010
how can we give link on add more option if we want to add more text box?
Raza23-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);
rajesh10-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 Agye06-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
Mikko06-05-2011
How can I add names to the options?
Santhosh K S23-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
Jim11-05-2012
Excellent. Simple, with a demo. Just what I needed. Thank you!
Devi02-01-2013
that totally works...simple , nice and easy
safina05-06-2014
in function addOption(selectbox,text,value ), can this selectbox be an id of a div for dropdown
Post Comment This is for short comments only. Use the forum for more discussions.
Name
Email( not to be displayed)Privacy Policy
1+2=This is to prevent automatic submission by spammers. Please enter the result of the sum as asked

Form Validation in JavaScript
Checkbox Validation of a form



HTML . MySQL. PHP. JavaScript. ASP. Photoshop. Articles. FORUM Contact us

©2000-2014 plus2net.com All rights reserved worldwide Privacy Policy Disclaimer