How to set an option value with vanilla JavaScript?

If you are trying to change the value, you are correct. bendqh1 is trying to select the option by value though, not change it’s value.

Changing the value of the select box (not the option), will select an option with a matching value, or if the option has no value the matching text content of the option.

<label for='selectBox'>Choose an option:</label>
<select name='selectBox' id='selectBox'>
  <option value='a'>option 1</option> <- 'a' 
  <option value=''>option 2</option> <- '' empty string
  <option value='b'>option 3</option> <- 'b'
  <option>option 4</option> <- 'option 4' text content
</select>

1 Like