You are here: Reference > JavaScript > client-side > HTML DOM > methods > namedItem (select)

namedItem method (select)

Browser support:
Returns an element or a collection of elements from the options collection of the current select element by name.
The namedItem method of a select element is identical to the namedItem method of the options collection of the select element.

Syntax:

object.namedItem (name);
You can find the related objects in the Supported by objects section below.

Parameters:

name
Required. String that specifies a value for the name or id property of the element or elements to retrieve.

Return value:

  • If no match is found, it returns null in Internet Explorer, Firefox and Opera, and returns undefined in Google Chrome and Safari.
  • If exactly one match is found, it returns the matching option element.
  • If more than one match is found, it returns an options sub-collection filled with the matching elements.

Example HTML code 1:

This example illustrates the use of the namedItem method:
<head>
    <script type="text/javascript">
        function GetOptionById () {
            var selectTag = document.getElementById ("fruit");
            var optionTag = selectTag.namedItem ("peach");
            alert ("The option with 'peach' ID is " + optionTag.text);
        }
    </script>
</head>
<body>
    <select id="fruit">
        <option id="apple">Apple</option>
        <option id="peach">Peach</option>
        <option id="pear">Pear</option>
    </select>
    <button onclick="GetOptionById ()">Get an option by ID</button>
</body>
Did you find this example helpful? yes no

Supported by objects:

Related pages:

External links:

User Contributed Comments

Post Content

Post Content