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

defaultSelected property (option)

Browser support:
Specifies or returns the initial state of an option element. The initial state can be set with the SELECTED attribute in HTML.
If you want to get or set the selected state of an option element, use the selected property.
The defaultChecked property can be useful if you want to detect whether the selected state of a control has been changed.

Syntax:

object.defaultSelected;
You can find the related objects in the Supported by objects section below.
This property is read-only.

Possible values:

Boolean that indicates the state of the option element.
One of the following values:
false
By default, the option is not selected.
true
By default, the option is selected.
Default: true.

Example HTML code 1:

This example illustrates the use of the defaultSelected property:
<head>
    <script type="text/javascript">
        function GetDefSelected () {
            var myOptions = document.getElementById ("mySelect").options;
            for (var i=0; i < myOptions.length; i++) {
                var isDefSelected = myOptions[i].defaultSelected;
                alert ("option " + i + " defaultSelected property= " + isDefSelected);
            }
        }
    </script>
</head>
<body>
    <button onclick="GetDefSelected ();">Get defaultSelected!</button>

    <select multiple="multiple" id="mySelect">
        <option>Apple</option>
        <option selected="selected">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