The defaultSelected
property returns the default value of the selected attribute.
defaultSelected |
Yes | Yes | Yes | Yes | Yes |
var v = optionObject.defaultSelected
A Boolean type value, true if the option is selected by default, otherwise false.
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: