The length property of the HTMLOptionsCollection interface returns the number of <option> elements in the collection. The property can get or set the size of the collection.
When setting length to a value smaller than the current, the options collection gets truncated; otherwise, new blank <option> elements are appended to the end of the <select>.
An integer value representing the number of items in this HTMLOptionsCollection.
const optCollection = document.getElementById("fruits").options;
const origLength = optCollection.length;
optCollection.length += 50; // adds 50 blank options to the collection
optCollection.length = origLength; // truncates the list back to the original size
| Specification |
|---|
| HTML> # dom-htmloptionscollection-length-dev> |
| Desktop | Mobile | |||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|
| Chrome | Edge | Firefox | Opera | Safari | Chrome Android | Firefox for Android | Opera Android | Safari on IOS | Samsung Internet | WebView Android | WebView on iOS | |
length |
1 | 12 | 1 | ≤12.1 | 3 | 18 | 4 | ≤12.1 | 1 | 1.0 | 4.4 | 1 |
HTMLOptionsCollection.add()HTMLOptionsCollection.remove()HTMLOptionsCollection.selectedIndexHTMLSelectElementHTMLOptGroupElementHTMLCollection.length
© 2005–2025 MDN contributors.
Licensed under the Creative Commons Attribution-ShareAlike License v2.5 or later.
https://developer.mozilla.org/en-US/docs/Web/API/HTMLOptionsCollection/length