Option defaultSelectedProperty

Option 对象参考手册Option Object

Definition and Usage

The defaultSelected property returns the initial value of the selected property.

Returns true if the default option is selected, otherwise returns false.

If the form is reset, the selected property is reset to the value of this property. This property also sets the value of the selected property.

Note:The default option will be displayed first.

Syntax

optionObject.defaultSelected


Browser Support

Internet ExplorerFirefoxOperaGoogle ChromeSafari

All major browsers support the defaultSelected property


Example

Example

The following example checks whether the selected item is the default option:

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Example(example.com)</title>
<script>
function displayResult(){
    var x=document.getElementById("mySelect").selectedIndex;
    var y=document.getElementsByTagName("option");
alert("" + y[x].text + " is the default option? " + y[x].defaultSelected);
}
</script>
</head>
<body>

<form>
Choose your favorite fruit:
<select id="mySelect">
    <option>Apple</option>
    <option>Orange</option>
    <option selected="selected">Pineapple</option>
    <option>Banana</option>
</select>
</form>
<button type="button" onclick="displayResult()">Is the selected fruit the default?</button>

</body>
</html>

Try it Yourself »


Option 对象参考手册Option Object Other Extensions