Javascript DOM HTML Option value Property set

Introduction

Change the value of a specific option element:

document.getElementById("myOption").value = "newValue";

Click the button to change the value of the apple option.

View in separate window

<!DOCTYPE html>
<html>
<body>

Select your favorite:
<select>
  <option id="myOption" value="apple">Apple</option>
  <option value="orange">Orange</option>
</select>/*w w  w  .  j a  v a  2 s.c  o  m*/
<button type="button" onclick="myFunction()">Test</button>

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

<script>
function myFunction() {
  document.getElementById("myOption").value = "newValue";
  document.getElementById("demo").innerHTML = "The value changed.";
}
</script>

</body>
</html>



PreviousNext

Related