Javascript DOM HTML Option text Property get

Introduction

Return the text of the selected option in a drop-down list:

var x = selTag.options[selTag.selectedIndex].text;

View in separate window

<!DOCTYPE html>
<html>
<body>

Select your favorite:
<select onchange="myFunction(this)">
  <option>Apple</option>
  <option>Orange</option>
  <option>Pineapple</option>
  <option>Banana</option>
</select>/*from  w w  w .j  a  v a 2 s. c om*/

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

<script>
function myFunction(selTag) {
  var x = selTag.options[selTag.selectedIndex].text;
  document.getElementById("demo").innerHTML = "You selected: " + x;
}
</script>

</body>
</html>



PreviousNext

Related