-
Notifications
You must be signed in to change notification settings - Fork 22.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
214 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
--- | ||
title: "HTMLOptionsCollection: add() method" | ||
short-title: add() | ||
slug: Web/API/HTMLOptionsCollection/add | ||
page-type: web-api-instance-method | ||
browser-compat: api.HTMLOptionsCollection.add | ||
--- | ||
|
||
{{APIRef("HTML DOM")}} | ||
|
||
The **`add()`** method of the {{DOMxRef("HTMLOptionsCollection")}} interface adds an {{domxref("HTMLOptionElement")}} or {{domxref("HTMLOptGroupElement")}} element to an `HTMLOptionsCollection`. | ||
|
||
By default, the `add()` appends the {{HTMLelement("option")}} or {{HTMLelement("optgroup")}} passed as the parameter to the end of the collection. You can define where the added `<option>` or `<optgroup>` should be placed by specifying the `before` parameter. The `before` is the `<option>` element or a numeric `0`-based index of the `<option>` element the added element should precede. | ||
|
||
If the `before` parameter is null or out of range (or omitted), the `<option>` or `<optgroup>` will be appended as the last element in the collection, outside of any {{HTMLelement("optgroup")}}. If the `<option>` referenced by the `before` parameter is in an {{HTMLelement("optgroup")}}, an added `HTMLOptionElement` will be in the same group. | ||
|
||
The `<optgroup>` element can only contain `<option>` elements as child nodes. The `add()` method will successfully add an `HTMLOptGroupElement` to the end of the `HTMLOptionsCollection` or between `<optgroup>` elements only. In other words, attempting to add an `HTMLOptGroupElement` before an `<option>` within an `<optgroup>` may silently if the `<option>` referenced by the `before` parameter is not the first `<option>` within its `<optgroup>`. | ||
|
||
## Syntax | ||
|
||
```js-nolint | ||
add(item) | ||
add(item, before) | ||
``` | ||
|
||
### Parameters | ||
|
||
- `item` | ||
- : An {{domxref("HTMLOptionElement")}} or {{domxref("HTMLOptGroupElement")}}. | ||
- `before` {{optional_inline}} | ||
- : An element of the collection, or a numeric 0-based index representing the element the _item_ should be inserted before. If omitted, `null`, or the index does not exist, the new element is appended to the end of the collection. | ||
|
||
### Return value | ||
|
||
None ({{jsxref("undefined")}}). | ||
|
||
### Exceptions | ||
|
||
- `HierarchyRequestError` {{DOMxRef("DOMException")}} | ||
- : Thrown if the _item_ passed to the method is an ancestor of the element into which it is to be inserted. | ||
|
||
## Examples | ||
|
||
```js | ||
const optionList = document.querySelector("select").options; | ||
const firstOption = document.createElement("option"); | ||
firstOption.text = "new item"; | ||
optionList.add(firstOption, 0); // added as the first item | ||
optionList.add(optionList[0]); // moves the first item to the end | ||
``` | ||
|
||
## Specifications | ||
|
||
{{Specifications}} | ||
|
||
## Browser compatibility | ||
|
||
{{Compat}} | ||
|
||
## See also | ||
|
||
- {{HTMLElement("select")}} | ||
- {{DOMxRef("HTMLOptionsCollection.remove")}} | ||
- {{DOMxRef("HTMLOptionsCollection.length")}} | ||
- {{DOMxRef("HTMLOptionsCollection.selectedIndex")}} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
--- | ||
title: "HTMLOptionsCollection: length property" | ||
short-title: length | ||
slug: Web/API/HTMLOptionsCollection/length | ||
page-type: web-api-instance-property | ||
browser-compat: api.HTMLOptionsCollection.length | ||
--- | ||
|
||
{{APIRef("DOM")}} | ||
|
||
The **`length`** property of the {{DOMxRef("HTMLOptionsCollection")}} interface returns the number of {{htmlelement("option")}} elements in the collection. The property can get or set the size of the collection. | ||
|
||
When setting `length`, property value assignment will truncate the `<option>` elements in the `<select>` if the value is smaller than the current `length` and will add new blank `<option>` elements to the `<select>` if the value is greater than the current `length`. | ||
|
||
## Value | ||
|
||
An integer value representing the number of items in this `HTMLOptionsCollection`. | ||
|
||
## Example | ||
|
||
```js | ||
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 | ||
``` | ||
|
||
## Specifications | ||
|
||
{{Specifications}} | ||
|
||
## Browser compatibility | ||
|
||
{{Compat}} | ||
|
||
## See also | ||
|
||
- {{DOMxRef("HTMLOptionsCollection.add()")}} | ||
- {{DOMxRef("HTMLOptionsCollection.remove()")}} | ||
- {{DOMxRef("HTMLOptionsCollection.selectedIndex")}} | ||
- {{domxref("HTMLSelectElement") }} | ||
- {{domxref("HTMLOptGroupElement")}} | ||
- {{domxref("HTMLCollection.length")}} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
--- | ||
title: "HTMLOptionsCollection: remove() method" | ||
short-title: remove() | ||
slug: Web/API/HTMLOptionsCollection/remove | ||
page-type: web-api-instance-method | ||
browser-compat: api.HTMLOptionsCollection.remove | ||
--- | ||
|
||
{{ APIRef("HTML DOM") }} | ||
|
||
The **`remove()`** method of the {{DOMxRef("HTMLOptionsCollection")}} interface removes the {{HTMLelement("option")}} element specified by the index from the ancestor {HTMLelement("select")}} element. | ||
|
||
## Syntax | ||
|
||
```js-nolint | ||
remove(index) | ||
``` | ||
|
||
### Parameters | ||
|
||
- `index` | ||
- : A zero-based integer for the index of the {{ domxref("HTMLOptionElement") }} in the {{DOMxRef("HTMLOptionsCollection")}}. If the index is not found the method has no effect. | ||
|
||
### Return value | ||
|
||
None ({{jsxref("undefined")}}). | ||
|
||
## Examples | ||
|
||
```js | ||
const optionList = document.querySelector("select").options; | ||
const listLength = optionList.length; | ||
optionList.remove(length - 1); // removes the last item | ||
optionList.remove(optionList[0]); // removes the first item | ||
``` | ||
|
||
## Specifications | ||
|
||
{{Specifications}} | ||
|
||
## Browser compatibility | ||
|
||
{{Compat}} | ||
|
||
## See also | ||
|
||
- {{DOMxRef("HTMLOptionsCollection.add")}} | ||
- {{DOMxRef("HTMLOptionsCollection.length")}} | ||
- {{DOMxRef("HTMLOptionsCollection.selectedIndex")}} | ||
- {{domxref("HTMLOptionsCollection") }} | ||
- {{domxref("HTMLOptionsCollection.remove()")}} | ||
- {{domxref("Element.remove")}} |
43 changes: 43 additions & 0 deletions
43
files/en-us/web/api/htmloptionscollection/selectedindex/index.md
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
--- | ||
title: "HTMLOptionsCollection: selectedIndex property" | ||
short-title: selectedIndex | ||
slug: Web/API/HTMLOptionsCollection/selectedIndex | ||
page-type: web-api-instance-property | ||
browser-compat: api.HTMLOptionsCollection.selectedIndex | ||
--- | ||
|
||
{{APIRef("HTML DOM")}} | ||
|
||
The **`selectedIndex`** property of the {{DOMxRef("HTMLOptionsCollection")}} interface reflects the index of the first selected | ||
{{HTMLElement("option")}} element, if any, or −1 if no `<option>` is selected. The property changes which option element is selected, or selects an option element if none is selected, with the `-1` deselecting any currently selected elements | ||
|
||
## Value | ||
|
||
A number. | ||
|
||
## Examples | ||
|
||
```js | ||
const optionColl = document.getElementById("select").options; | ||
console.log(`selected option: ${optionColl.selectedIndex}`); // the index of the first selected option, or -1 if no option is selected | ||
optionColl.selectedIndex = 0; // selects the first item | ||
optionColl.selectedIndex = -1; // deselects any selected option | ||
``` | ||
|
||
## Specifications | ||
|
||
{{Specifications}} | ||
|
||
## Browser compatibility | ||
|
||
{{Compat}} | ||
|
||
## See also | ||
|
||
- {{DOMxRef("HTMLOptionsCollection.length")}} | ||
- {{DOMxRef("HTMLOptionsCollection.add()")}} | ||
- {{DOMxRef("HTMLOptionsCollection.remove()")}} | ||
- {{DOMxRef("HTMLOptionsCollection")}} | ||
- {{DOMxRef("HTMLOptionElement")}} | ||
- {{DOMxRef("HTMLOptGroupElement")}} | ||
- {{DOMxRef("HTMLSelectElement")}} |