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

item method (select)

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

Syntax:

object.item (index [, subIndex]);
You can find the related objects in the Supported by objects section below.

Parameters:

index
Required. String or zero-based integer that specifies the element or elements to retrieve.
  • If an integer value is specified, it specifies the index of the element to retrieve. The first index is zero.
  • If this parameter is a string, then it specifies a value for the name or id property of the element or elements to retrieve.
subIndex
Optional. Supported by Internet Explorer only.
If more than one matching element is found for the index parameter (possible only if it is a string), the item method creates an options sub-collection filled with the matching elements. In that case, the subIndex property specifies the position of the element in the sub-collection to retrieve. In other cases, the subIndex property has no meaning.

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 item method:
<head>
    <script type="text/javascript">
        function GetSelOption (selectTag) {
            // Returns the index of the selected option
            var selIdx = selectTag.selectedIndex;
            var selOption = selectTag.item (selIdx);
            alert ("The selected option is " + selOption.text);
        }
    </script>
</head>
<body>
    Change the selection in the list below:
    <select onchange="GetSelOption (this);">
        <option>Apple</option>
        <option>Peach</option>
        <option>Pear</option>
    </select>
</body>
Did you find this example helpful? yes no

Supported by objects:

Related pages:

External links:

User Contributed Comments

Post Content

Post Content