HTMLSelectElement.namedItem()
The HTMLSelectElement.namedItem()
method returns the HTMLOptionElement
corresponding to the HTMLOptionElement
whose name
or id
match the specified name, or null
if no option matches.
In JavaScript, using the array bracket syntax with a String
, like selectElt["value"]
is equivalent to selectElt``.namedItem("value")
.
Syntax
var item = collection.namedItem(str); var item = collection[str];
Parameters
-
str
is aDOMString
.
Return value
-
item
is aHTMLOptionElement
.
Example
HTML
<form> <select id="myFormControl"> <option id="o1">Opt 1</option> <option id="o2">Opt 2</option> </select> </form>
JavaScript
elem1 = document.forms[0]['myFormControl']['o1']; // Returns the HTMLOptionElement representing #o1
Specifications
Specification |
---|
HTML Standard (HTML) # dom-select-nameditem-dev |
Browser compatibility
Desktop | Mobile | |||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|
Chrome | Edge | Firefox | Internet Explorer | Opera | Safari | WebView Android | Chrome Android | Firefox for Android | Opera Android | Safari on IOS | Samsung Internet | |
namedItem |
1 |
12
namedItem does not appear to take the name attribute into account (only the id attribute) on Internet Explorer and Edge. There is a bug report to Microsoft about this. |
1 |
6
namedItem does not appear to take the name attribute into account (only the id attribute) on Internet Explorer and Edge. There is a bug report to Microsoft about this. |
≤12.1 |
3 |
1 |
18 |
4 |
≤12.1 |
1 |
1.0 |
See also
-
HTMLSelectElement
that implements it.
© 2005–2021 MDN contributors.
Licensed under the Creative Commons Attribution-ShareAlike License v2.5 or later.
https://developer.mozilla.org/en-US/docs/Web/API/HTMLSelectElement/namedItem