You are here: Reference > JavaScript > client-side > HTML DOM > properties > selectedIndex (options, keygen, select)
selectedIndex property (options, keygen, select)
Specifies or returns the zero-based index of the selected option in a select object or in the selection list of a keygen object.
If multiple selection is allowed in the select object, than the selectedIndex property returns the index of
the first selected option. If no option is selected, the selectedIndex property contains -1.
If you want to specify only one selected element in a multiple-selection list or a selected element in a single-selection list, you can do it in the following ways:
Syntax:
You can find the related objects in the Supported by objects section below.
This property is read/write.
Possible values:
Zero-based Integer, the selected option.
Default: this property has no default value.
Example HTML code 1:
This example illustrates the use of the selectedIndex property:
|
||||
<head> <script type="text/javascript"> function GetSelOption (selectTag) { var selIdx = selectTag.selectedIndex; var selOption = selectTag.options[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?
|
Supported by objects:
Related pages:
External links:
User Contributed Comments