Javascript Reference - HTML DOM Select remove() Method








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

Browser Support

remove Yes Yes Yes Yes Yes

Syntax

selectObject.remove(index) 

Parameter Values

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




Return Value

No return value.

Example

The following code shows how to Remove the option with index "2" from a drop-down list.


<!DOCTYPE html>
<html>
<body>
<!--from w w  w  .j a v a 2 s  .c  o m-->
<form>
<select id="mySelect" size="4">
  <option>A</option>
  <option>B</option>
  <option>C</option>
  <option>D</option>
</select>
</form>
<br>
<button onclick="myFunction()">Remove option with index "2"</button>

<script>
function myFunction() {
    var x = document.getElementById("mySelect");
    x.remove(2);
}
</script>

</body>
</html>

The code above is rendered as follows:





Example 2

The following code shows how to remove the last option from a drop-down list.


<!DOCTYPE html>
<html>
<body>
<!--   www. j av a 2  s.  c  o  m-->
<form>
<select id="mySelect" size="4">
  <option>A</option>
  <option>B</option>
  <option>C</option>
  <option>D</option>
  <option>E</option>
  <option>F</option>
</select>
</form>
<button onclick="myFunction()">test</button>
<script>
function myFunction() {
    var x = document.getElementById("mySelect");
    if (x.length > 0) {
        x.remove(x.length-1);
    }
}
</script>
</body>
</html>

The code above is rendered as follows:

Example 3

The following code shows how to remove the selected option from the drop-down list.


<!DOCTYPE html>
<html>
<body>
<!--   w ww . j a v a  2 s.  c  o  m-->
<form>
Select a fruit:
<br>
<select id="mySelect" size="4">
  <option>A</option>
  <option>B</option>
  <option>C</option>
  <option>D</option>
  <option>E</option>
</select>
</form>
<button onclick="myFunction()">test</button>

<script>
function myFunction() {
    var x = document.getElementById("mySelect");
    x.remove(x.selectedIndex);
}
</script>

</body>
</html>

The code above is rendered as follows: