#webdevelopment
#coding
#dropdown menu
#get value
#javascript

How can I get the selected value in a dropdown list using JavaScript?

Anonymous

AnonymousJan 06, 2024

In this post, we will learn how to get the selected value of the dropdown list using javascript.

HTML

<select id="myDropdown">
    <option value="option1">Option 1</option>
    <option value="option2">Option 2</option>
    <option value="option3">Option 3</option>
</select>

Javscript

let dropdown = document.getElementById("myDropdown");
dropdown.addEventListener("change", function () {
  let selectedValue = dropdown.value;
  console.log("The Selected value is: " + selectedValue);
});

The console output will be:

let's say the user opens the page, and they choose "Option 2" & "Option 3" from the dropdown.

Happy Coding! ❤️