Adding 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 an Option object
var opt = document.createElement("option");
// Add an Option object to Drop Down/List Box
document.getElementById("DropDownList").options.add(opt);
// Assign text and value to Option object
opt.text = Text;
opt.value = Value;
}
<script />
You can use this function in for loop to add more than one item.
Getting the selected Value of dropdownlist using javascript
var roles = document.getElementById("DropDownList ");
if(roles!=null&& roles.selectedIndex!=-1){
//Getting value
rolesVal=roles.options[roles.selectedIndex].value;
//Getting text
rolesVal=roles.options[roles.selectedIndex].text;
}
Removing all the items of dropdownlist using javascript
var regions = document.getElementById("DropDownList ");
var len = regions.options.length;
for(i=len-1;i>=0;i--){
regions.options.remove(i);
}