Javascript DOM HTML Select value Property get

Introduction

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

var x = document.getElementById("mySelect").value;

Click the button to return the value of the selected fruit.

View in separate window

<!DOCTYPE html>
<html>
<body>

<form>
  Select your favorite:
  <select id="mySelect">
    <option value="apple">Apple</option>
    <option value="orange">Orange</option>
    <option value="pineapple">Pineapple</option>
    <option value="banana">Banana</option>
  </select>
</form>/*from   w ww .ja  v a  2  s. c  o m*/


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

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

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

</body>
</html>



PreviousNext

Related