How to Get Selected Value in Dropdown List Using Javascript

Avatar

By squashlabs, Last Updated: Nov. 25, 2023

How to Get Selected Value in Dropdown List Using Javascript

To get the selected value in a dropdown list using JavaScript, you can use the following approaches:

1. Using the value property of the selected option

You can access the selected value of a dropdown list by using the value property of the selected option. Here's an example:

// HTML

  Option 1
  Option 2
  Option 3


// JavaScript
const dropdown = document.getElementById("myDropdown");
const selectedValue = dropdown.value;
console.log(selectedValue); // Output: "option2"

In the above example, we first obtain a reference to the dropdown element using its ID. Then, we access the value property of the dropdown element to retrieve the selected option's value. Finally, we log the selected value to the console.

Related Article: How to Use Angular Reactive Forms

2. Using the selectedIndex property and options collection

Another way to get the selected value in a dropdown list is by using the selectedIndex property and the options collection. Here's an example:

// HTML

  Option 1
  Option 2
  Option 3


// JavaScript
const dropdown = document.getElementById("myDropdown");
const selectedOption = dropdown.options[dropdown.selectedIndex];
const selectedValue = selectedOption.value;
console.log(selectedValue); // Output: "option2"

In this example, we first obtain a reference to the dropdown element using its ID. Then, we use the selectedIndex property to get the index of the selected option. We can then access the selected option using the options collection and the obtained index. Finally, we retrieve the value property of the selected option and log it to the console.

Best Practices

Related Article: How To Use the Javascript Void(0) Operator

- It's important to ensure that the JavaScript code is executed after the DOM has finished loading. You can achieve this by placing your code inside a DOMContentLoaded event listener or by placing the script tag at the end of the HTML body.

- When selecting elements by ID, make sure the ID is unique within the document. Otherwise, the code may not select the intended element.

- To improve code readability and maintainability, consider assigning the dropdown element to a variable with a meaningful name.

- When accessing the selected value multiple times, it's more efficient to store it in a variable rather than repeatedly accessing the DOM.

These are the main ways to get the selected value in a dropdown list using JavaScript. Choose the approach that best suits your needs and coding style.

You May Also Like

How to Determine If Checkbox Is Checked in jQuery

Identifying if a checkbox is checked in jQuery can be easily done using the prop() method or the is() method. This technical guide provides an overvi… read more

Tutorial: JavaScript in AJAX Technology

JavaScript is a crucial component of AJAX technology, enabling dynamic web interactions. This article explores the role of JavaScript in AJAX, examin… read more

How to Convert Date to Timestamp in Javascript

Converting date objects to timestamps in JavaScript can be done using the getTime() method or the valueOf() method. This article provides a step-by-s… read more

How to Fetch Post JSON Data in Javascript

Learn how to fetch post JSON data in Javascript using the Fetch API. Discover an alternative approach using XMLHttpRequest and explore best practices… read more

How to Convert an Object to a String in JavaScript

Converting an object to a string in JavaScript can be done using various methods. One approach is to use the JSON.stringify() method, which converts … read more

How to Add JavaScript Code in Angular Components

The appropriate placement of JavaScript within an Angular component is crucial for effective web development. This article explores best practices, r… read more

How to Convert Array to JSON in JavaScript

Converting an array into a JSON object is a simple process using JavaScript. This article provides step-by-step instructions on how to achieve this u… read more

Creating Web Workers with NextJS in JavaScript

Web workers are a powerful tool in JavaScript for improving web application performance. In this article, you will learn how to create web workers us… read more

Executing a Local NextJS Build in JavaScript

Learn the steps to perform a NextJS build locally in JavaScript without complications. Discover how to install Next.js, set up a development environm… read more

How to Write Asynchronous JavaScript with Promises

Asynchronous JavaScript programming can be made more with the use of Promises. This article will guide you through the process of using Promises, und… read more