JAVASCRIPT - HOW TO Clear ALL Options of Select - Javascript Source Code

JAVASCRIPT - HOW TO Clear ALL the Options or List Elements of an HTML Select Box with One Line of Javascript Code.

Home Short:

Clearing or removing all the options or list items from a select box/listbox in Javascript is easily accomplished with one short line of Javascript code.

If you want to remove only specific items from an HTML select box then ...

document.getElementById('idOfSelect').options.remove(indexOfItem); replacing indexOfItem with the index number of the select option you want to remove, ie ...

... the above Javascript code would remove the second option element from the HTML select element with an id of idOfSelect.

Try it yourself. Click the button to clear all the options from the HTML select box below. Right click this page and click VIEW SOURCE to view the code associated with clearing all options from HTML select element.



S
H
A
R
E