How to remove an item from a select box with JavaScript?

Spread the love

Sometimes, we want to remove an item from a select box with JavaScript.

In this article, we’ll look at how to remove an item from a select box with JavaScript.

How to remove an item from a select box with JavaScript?

To remove an item from a select box with JavaScript, we use the remove method.

For instance, we write

<select name="selectBox" id="selectBox">
  <option value="option1">option1</option>
  <option value="option2">option2</option>
  <option value="option3">option3</option>
  <option value="option4">option4</option>
</select>

to add a select drop down.

Then we write

const selectBox = document.getElementById("selectBox");
document.querySelector('#selectBox option:checked').remove();

to select the select element with getElementById.

Then we select the selected option element with document.querySelector('#selectBox option:checked').

And we call remove to remove it.

Conclusion

To remove an item from a select box with JavaScript, we use the remove method.

Leave a Reply

Your email address will not be published. Required fields are marked *