HTMLSelectElement: namedItem() method
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 selectElt.namedItem('value')
is equivalent to selectElt.options.namedItem('value')
.
Syntax
Parameters
Return value
Examples
HTML
<form>
<select id="myFormControl">
<option id="o1">Opt 1</option>
<option id="o2">Opt 2</option>
</select>
</form>
JavaScript
let selectElt = document.getElementById("myFormControl");
elem1 = selectElt.namedItem("o1");
But, you cannot write:
let selectElt = document.getElementById("myFormControl");
elem1 = selectElt.o1;
elem1 = selectElt["o1"];
Specifications
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 |
12namedItem 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 |
6namedItem 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 |
4.4 |
18 |
4 |
≤12.1 |
1 |
1.0 |
See also