Select remove() Method - Javascript DOM HTML Element

Javascript examples for DOM HTML Element:Select

Description

The remove() method removes an option from a drop-down list.

Parameter Values

Parameter Description
index Required. index of the option to remove. Index starts at 0

Return Value:

No return value

The following code shows how to Remove the selected option from the drop-down list:

Demo Code

ResultView the demo in separate window

<!DOCTYPE html>
<html>
<body>

<form>
<select id="mySelect" size="4">
  <option>Apple</option>
  <option>Pear</option>
  <option>Banana</option>
  <option>Orange</option>
</select>/*from ww w  . j  a  v  a  2  s. co m*/
</form>
<br>

<button onclick="myFunction()">Remove last option</button>

<script>
function myFunction() {
    var x = document.getElementById("mySelect");
    if (x.length > 0) {
        x.remove(x.length-1);
    }
}
</script>

</body>
</html>

Related Tutorials