dom / latest / htmlselectelement / nameditem.html /

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 selectElt.namedItem('value') is equivalent to selectElt.options.namedItem('value').

Syntax

namedItem(str)

Parameters

  • str is a string.

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'); // Returns the HTMLOptionElement representing #o1

But, you cannot write:

let selectElt = document.getElementById('myFormControl');
elem1 = selectElt.o1; // Returns undefined
elem1 = selectElt['o1']; // Returns undefined

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
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

© 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