Javascript DOM HTML Option selected Property get

Introduction

Find out if the "banana" option in a drop-down list is selected:

var x = document.getElementById("banana").selected;

Click the button to return whether the banana option is selected.

View in separate window

<!DOCTYPE html>
<html>
<body>

Select your favorite:
<br>
<select size="4">
  <option id="apple" selected>Apple</option>
  <option id="orange">Orange</option>
  <option id="pineapple">Pineapple</option>
  <option id="banana">Banana</option>
</select>/*from  w  w w.  j a v a2s  .  c  o  m*/
<button onclick="myFunction()">Test</button>

<p id="demo"></p>

<script>
function myFunction() {
  var x = document.getElementById("banana").selected;
  document.getElementById("demo").innerHTML = x;
}
</script>

</body>
</html>



PreviousNext

Related