The remove()
method can remove an option from a drop-down list.
remove |
Yes | Yes | Yes | Yes | Yes |
selectObject.remove(index)
Parameter | Description |
---|---|
index | Required. The index of the option to remove. Index starts at 0 |
No return value.
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:
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:
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: