Javascript DOM HTML Option defaultSelected Property get

Introduction

Check if the selected option is checked by default:

View in separate window

<!DOCTYPE html>
<html>
<body>

Select your favorite:
<select id="mySelect">
  <option>Apple</option>
  <option>Orange</option>
  <option selected>Pineapple</option>
  <option>Banana</option>
</select>//from w  w w . j a  v a  2  s  .c  o m
<br>
<p id="demo"></p>
<button type="button" onclick="myFunction()">Is selected fruit selected by default?</button>

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

</body>
</html>

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

This property returns true if an option is selected by default, otherwise it returns false.

The defaultSelected property returns true if the option is selected by default, otherwise it returns false




PreviousNext

Related