Javascript DOM HTML Select value Property set

Introduction

Change the selected option to "banana":

document.getElementById("mySelect").value = "banana";

Click the button to change the selected fruit to banana.

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  w w.ja va2  s  .  co  m
<button type="button" onclick="myFunction()">Test</button>

<script>
function myFunction() {
  document.getElementById("mySelect").value = "banana";
}
</script>

</body>
</html>

The value property sets or gets the value of the selected option in a drop-down list.

The value property accepts and returns a String type value.

If the drop-down list allows multiple selections, the first selected option is returned.




PreviousNext

Related