You are here: Reference > JavaScript > client-side > HTML DOM > properties > index (optgroup, option)

index property (optgroup, option)

Browser support:
Specifies or returns an integer that indicates the position of an option element within a selection list.
The position of an option in a selection list and in the options collection of the selection list is identical.

Syntax:

object.index;
You can find the related objects in the Supported by objects section below.
This property is read/write.

Possible values:

Zero-based integer that sets or retrieves the ordinal position.
Default: this property has no default value.

Example HTML code 1:

This example illustrates the use of the index property:
<head>
    <script type="text/javascript">
        function GetOrdPos (selectTag) {
            var selIndexes = "";

            for (var i = 0; i < selectTag.options.length; i++) {
                var optionTag = selectTag.options[i];
                if (optionTag.selected) {
                    if (selIndexes.length > 0)
                        selIndexes += ", ";
                    selIndexes += optionTag.index;
                }
            }

            var info = document.getElementById ("info");
            if (selIndexes.length > 0) {
                info.innerHTML = "The indices of the selected options: " + selIndexes;
            }
            else {
                info.innerHTML = "There is no selected option";
            }
        }
    </script>
</head>
<body>
    Please select some options (use the CTRL key for multiple selection)
    <br />
    <select multiple="multiple" onchange="GetOrdPos (this);">
        <option>Apple</option>
        <option>Peach</option>
        <option>Pear</option>
    </select>
    <br /><br />

    <span id="info" style="border:1px solid #606060; background-color:#e0d0e0;"></span>
</body>
Did you find this example helpful? yes no

Supported by objects:

Related pages:

External links:

User Contributed Comments

Post Content

Post Content