Wednesday, June 06, 2007

Add items in Drop Down List or List Box using Javascript

        I have seen lots of questions in diffrent forums for adding items in drop down list or list box using javascript. Below is the script for the same.

 

<script type="text/javascript">
function AddItem(Text,Value)
{
// Create Option object
var opt = document.createElement("option");



// Add Option to Dropdown/Listbox
document.getElementById
("DropDownList").options.add(opt);

// Assign text and value to option
opt.text = Text;
opt.value = Value;


}
<script />
        You can use this function in for loop and add more than one Items.
Happy Programming !!

2 comments:

Anonymous said...

I had the same problem before couple of months. I had tried the same. But I wanted to know that during the postback newly added item still be there in dropdownlist or not ?

Unknown said...

its not working in mozilla firefox.