Javascript DOM HTML Option label Property display the selected label

Introduction

Alert the label value of the selected option in a drop-down list:

Click the button to return the label value of the selected car.

View in separate window

<!DOCTYPE html>
<html>
<body>

Select your favorite:
<br>
<select id="mySelect" size="4">
  <option label="javascript">Javascript</option>
  <option label="html">HTML</option>
  <option label="c++">C++</option>
  <option label="sql">SQL</option>
</select>/* w  w w . j a  v  a  2  s.  c o m*/
<p id="demo"></p>

<button type="button" onclick="myFunction()">Test</button>

<script>
function myFunction() {
  var x = document.getElementById("mySelect").selectedIndex;
  document.getElementById("demo").innerHTML = =document.getElementsByTagName("option")[x].label;
}
</script>

</body>
</html>



PreviousNext

Related