-
Notifications
You must be signed in to change notification settings - Fork 23.2k
Expand file tree
/
Copy pathindex.md
More file actions
71 lines (49 loc) · 1.39 KB
/
index.md
File metadata and controls
71 lines (49 loc) · 1.39 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
---
title: "HTMLSelectElement: namedItem() method"
short-title: namedItem()
slug: Web/API/HTMLSelectElement/namedItem
page-type: web-api-instance-method
browser-compat: api.HTMLSelectElement.namedItem
---
{{ APIRef("HTML DOM") }}
The **`HTMLSelectElement.namedItem()`** method returns the
{{domxref("HTMLOptionElement")}} corresponding to the {{domxref("HTMLOptionElement")}}
whose `name` or `id` match the specified name, or
`null` if no option matches.
In JavaScript, using `selectElt.namedItem('value')` is equivalent to `selectElt.options.namedItem('value')`.
## Syntax
```js-nolint
namedItem(str)
```
### Parameters
- `str`
- : A string representing the `name` or `id` of the option.
### Return value
An {{domxref("HTMLOptionElement")}} or `null`.
## Examples
### HTML
```html
<form>
<select id="myFormControl">
<option id="o1">Opt 1</option>
<option id="o2">Opt 2</option>
</select>
</form>
```
### JavaScript
```js
let selectElt = document.getElementById("myFormControl");
elem1 = selectElt.namedItem("o1"); // Returns the HTMLOptionElement representing #o1
```
But, you cannot write:
```js
let selectElt = document.getElementById("myFormControl");
elem1 = selectElt.o1; // Returns undefined
elem1 = selectElt["o1"]; // Returns undefined
```
## Specifications
{{Specifications}}
## Browser compatibility
{{Compat}}
## See also
- {{domxref("HTMLSelectElement")}} that implements it.