DEV Community

Avnish
Avnish

Posted on

Set the Value of a Select Element using JavaScript

Title : Set the Value of a Select Element using JavaScript

Example 1: Set the value of a select element to a specific option

<select id="mySelect">
  <option value="apple">Apple</option>
  <option value="banana">Banana</option>
  <option value="orange">Orange</option>
</select>

<script>
const selectElement = document.getElementById('mySelect');
selectElement.value = 'banana';
</script>
Enter fullscreen mode Exit fullscreen mode

Explanation:

  1. We have a select element with id mySelect containing three options: Apple, Banana, and Orange.
  2. We use document.getElementById() to get the select element.
  3. We set the value of the select element to 'banana' by assigning it to the value property of the select element.

Output:
The select element will have Banana selected.

Example 2: Remove the selection from a select element

<select id="mySelect">
  <option value="apple">Apple</option>
  <option value="banana">Banana</option>
  <option value="orange">Orange</option>
</select>

<script>
const selectElement = document.getElementById('mySelect');
selectElement.value = ''; // Removing the selection
</script>
Enter fullscreen mode Exit fullscreen mode

Explanation:

  1. We have a select element with id mySelect containing three options: Apple, Banana, and Orange.
  2. We use document.getElementById() to get the select element.
  3. We set the value of the select element to an empty string ('') to remove the selection.

Output:
The select element will have no option selected.

Example 3: Get an array of values of all option elements

<select id="mySelect">
  <option value="apple">Apple</option>
  <option value="banana">Banana</option>
  <option value="orange">Orange</option>
</select>

<script>
const selectElement = document.getElementById('mySelect');
const optionValues = Array.from(selectElement.options).map(option => option.value);
console.log(optionValues);
</script>
Enter fullscreen mode Exit fullscreen mode

Explanation:

  1. We have a select element with id mySelect containing three options: Apple, Banana, and Orange.
  2. We use document.getElementById() to get the select element.
  3. We convert the options HTMLCollection into an array using Array.from().
  4. We use the map() method to iterate over each option and extract its value.
  5. We log the array of option values to the console.

Output:

["apple", "banana", "orange"]
Enter fullscreen mode Exit fullscreen mode

Example 4: Change the value of a select element dynamically

<select id="mySelect">
  <option value="red">Red</option>
  <option value="blue">Blue</option>
  <option value="green">Green</option>
</select>

<script>
const selectElement = document.getElementById('mySelect');
selectElement.value = 'green'; // Changing the value dynamically
</script>
Enter fullscreen mode Exit fullscreen mode

Explanation:

  1. We have a select element with id mySelect containing three options: Red, Blue, and Green.
  2. We use document.getElementById() to get the select element.
  3. We set the value of the select element to 'green' to dynamically change the selected option.

Output:
The select element will have Green selected.

Example 5: Using a loop to set the value of a select element

<select id="mySelect">
  <option value="1">One</option>
  <option value="2">Two</option>
  <option value="3">Three</option>
</select>

<script>
const selectElement = document.getElementById('mySelect');
const newValue = '3'; // New value to set

for (let option of selectElement.options) {
  if (option.value === newValue) {
    option.selected = true;
    break; // Exit the loop once the value is set
  }
}
</script>
Enter fullscreen mode Exit fullscreen mode

Explanation:

  1. We have a select element with id mySelect containing three options: One, Two, and Three.
  2. We use document.getElementById() to get the select element.
  3. We define a new value ('3') to set.
  4. We iterate over each option using a loop.
  5. If the value of the current option matches the new value, we set its selected property to true.
  6. We use break to exit the loop once the value is set.

Output:
The select element will have Three selected.

These examples demonstrate different ways to set the value of a select element dynamically using JavaScript. Each method provides flexibility in selecting options based on specific criteria or requirements.

Top comments (0)