How to Get the Selected Value in a Dropdown List Using JavaScript?


Problem Statement

Retrieving the selected value from a dropdown list is a common requirement in web development, used in forms, filters, and dynamic interfaces. How can we efficiently access the selected option's value and display text using JavaScript?

Solution Code

Here’s a simple example to retrieve the selected value and text from a dropdown:

HTML

<form>
  <select id="ddlViewBy">
    <option value="1">Option 1</option>
    <option value="2" selected>Option 2</option>
    <option value="3">Option 3</option>
  </select>
</form>

JavaScript

function getSelectedOption() {
  // Get the dropdown element
  const dropdown = document.getElementById("ddlViewBy");
  // Get the selected value and text
  const value = dropdown.value; // Selected value
  const text = dropdown.options[dropdown.selectedIndex].text; // Selected text
  console.log("Selected Value:", value); // Logs "2"
  console.log("Selected Text:", text);  // Logs "Option 2"
  return { value, text }; // Return as an object for further use
}

// Example usage 
getSelectedOption();  

Learn More

Alternative Methods

  1. Using querySelector:
const dropdown = document.querySelector("#ddlViewBy");
const value = dropdown.value;
const text = dropdown.options[dropdown.selectedIndex].text;
  1. Directly Accessing the Value via Event Listeners:
document.getElementById("ddlViewBy").addEventListener("change", (event) => {
  console.log("Selected Value:", event.target.value);
  console.log("Selected Text:", event.target.options[event.target.selectedIndex].text);
});

  1. For Multi-Select Dropdowns:
    Loop through the options to find selected ones:
  const dropdown = document.getElementById("multiSelect");
const selectedOptions = Array.from(dropdown.options)
  .filter(option => option.selected)
  .map(option => ({ value: option.value, text: option.text }));
console.log(selectedOptions);

Summary

  • Use document.getElementById or querySelector for efficient selection of dropdown elements.
  • Access the value and text properties to retrieve the selected option.
  • Use event listeners to dynamically handle changes in selection.
  • For multi-select dropdowns, filter the selected options.

References


Quick Recap of efficiently retrieving the selected value from a dropdown list using JavaScript

Thank you for taking the time to read this article and expanding your knowledge on how to efficiently retrieve the selected value and text from a dropdown list using JavaScript. This fundamental skill is essential for building dynamic and interactive web applications.

To further enhance your understanding, here are additional topics you might find helpful for future exploration:

  1. Accessibility Best Practices: Learn how to make dropdowns more inclusive using ARIA roles, labels, and keyboard navigation.
  2. Dynamically Populating Dropdowns: Discover how to generate options dynamically, including fetching data from APIs.
  3. Validation Techniques: Ensure proper selection and handle validation logic effectively.
  4. Styling Custom Dropdowns: Master CSS and JavaScript techniques to style dropdowns for a unique user experience.
  5. Event Handling: Understand advanced scenarios like user-triggered vs. programmatic changes.
  6. Working with Frameworks: Manage dropdowns in popular frameworks such as React, Angular, or Vue.js.
  7. Cross-Browser Compatibility: Address browser-specific issues for a seamless user experience.
  8. Alternative UI Components: Explore modern alternatives like comboboxes or autocomplete widgets.

The journey of mastering JavaScript and building user-friendly web applications requires continuous learning. By expanding your expertise in these areas, you can elevate your development skills and create even more engaging web solutions.


Compiled by team Crio.Do