Javascript DOM HTML Option value Property get

Introduction

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

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

View in separate window

<!DOCTYPE html>
<html>
<body>

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>//w  w w.ja  v  a  2s.  c  om
<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].value;
}
</script>

</body>
</html>

The value property sets or gets the value of the option.

The value is sent to the server when the form is submitted.

If the value property is not set for an option element, the text content will be sent to the server.

The value property accepts and returns a String type value.




PreviousNext

Related