Javascript DOM HTML Option text Property get text for all options

Introduction

Get the text of all options in a drop-down list:

Click the button to display the text of all options in the dropdown list.

View in separate window

<!DOCTYPE html>
<html>
<body>

Select your favorite:
<select id="mySelect">
  <option>Apple</option>
  <option>Orange</option>
  <option>Pineapple</option>
  <option>Banana</option>
</select>/*from  w  w w. j  ava  2s  .co m*/
<p id="demo"></p>
<button type="button" onclick="myFunction()">Test</button>

<script>
function myFunction() {
  var x = document.getElementById("mySelect");
  var txt = "All options: <br/>";
  var i;
  for (i = 0; i < x.length; i++) {
    txt = txt + "<br/>" + x.options[i].text;
  }

  document.getElementById("demo").innerHTML = txt;
}
</script>

</body>
</html>



PreviousNext

Related