Skip to main content

How to remove a option from Select box using javascript?



function deleteOption(id) {
listElem = document.getElementById(id);
// id of the select box in the html

if(listElem.selectedIndex >= 0)

listElem.removeChild(listElem.options[listElem.selectedIndex]);
//selected item in the select box will be removed

if( listElem.options.length == 0 ){

var newOption = document.createElement("OPTION");

newOption.text = "Select One";
// if all the members are removed add default text

newOption.value = "Select One";
//default value for the default option text

listElem.add(newOption);

}
}

Comments