How to programmatically set the value of a select box element using JavaScript?

We can set the value of a select box using Javascript using the following. Suppose we have the following select box −

To set the value of this select element, we need to access it using querySelector. Then set the value. For example −

Example

// Search the select box
const mySelectBox = document.querySelector('#my-select');
// Set the value to 3 or Strawberry
mySelectBox.value = 3;
Updated on: 2019-11-27T10:39:19+05:30

1K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements