Javascript Reference - HTML DOM Option defaultSelected Property








The defaultSelected property returns the default value of the selected attribute.

Browser Support

defaultSelected Yes Yes Yes Yes Yes

Syntax

var v = optionObject.defaultSelected 

Return Value

A Boolean type value, true if the option is selected by default, otherwise false.





Example

The following code shows how to check if the selected option is checked by default.


<!DOCTYPE html>
<html>
<body>
<!--from   ww  w.j a  v a 2s.  c  o m-->
Select your favorite fruit:
<select id="mySelect">
  <option>A</option>
  <option>B</option>
  <option selected>C</option>
  <option>D</option>
</select>
<br>

<button type="button" onclick="myFunction()">test</button>

<script>
function myFunction() {
    var x = document.getElementById("mySelect").selectedIndex;
    var y = document.getElementsByTagName("option");
    console.log("Is " + y[x].text + " selected by default? " + y[x].defaultSelected);
}
</script>

</body> 
</html>

The code above is rendered as follows: