Categories
JavaScript Answers

How to remove all option of dropdown box in JavaScript?

Spread the love

Sometimes, we want to remove all option of dropdown box in JavaScript.

In this article, we’ll look at how to remove all option of dropdown box in JavaScript.

How to remove all option of dropdown box in JavaScript?

To remove all option of dropdown box in JavaScript, we can set the options.length property of the select element to 0.

For instance, we write:

<select>
  <option>foo</option>
  <option>bar</option>
</select>

to add the select drop down.

Then we write:

document.querySelector('select').options.length = 0;

to remove all the option elements from the drop down.

We select the select element with document.querySelector('select').

And then we set the options.length property of it to 0 to remove all the options.

Conclusion

To remove all option of dropdown box in JavaScript, we can set the options.length property of the select element to 0.

By John Au-Yeung

Web developer specializing in React, Vue, and front end development.

Leave a Reply

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