Open In App
Related Articles

How to get selected value in dropdown list using JavaScript ?

Improve Article
Improve
Save Article
Save
Like Article
Like

In this article, we will learn to get the selected values in the dropdown list in Javascript. The dropdown list is created using the <select> tab with <option> tab and for selecting the value we perform different methods.

We can get the values using 2 methods:

Method 1: Using the value property

  • The value of the selected element can be found by using the value property on the selected element that defines the list.
  • This property returns a string representing the value attribute of the <option> element in the list.
  • If no option is selected then nothing will be returned.

Example: This example is the implementation of the above-explained approaches.

HTML




<!DOCTYPE html>
<html>
 
<head>
    <title>
        How to get selected value in
        dropdown list using JavaScript?
    </title>
</head>
 
<body>
    <h1 style="color: green">
        GeeksforGeeks
    </h1>
    <b>
        How to get selected value in dropdown
        list using JavaScript?
    </b>
    <p> Select one from the given options:
        <select id="select1">
            <option value="free">Free</option>
            <option value="basic">Basic</option>
            <option value="premium">Premium</option>
        </select>
    </p>
 
 
    <p> The value of the option selected is:
        <span class="output"></span>
    </p>
 
 
    <button onclick="getOption()"> Check option </button>
 
    <script type="text/javascript">
        function getOption() {
            selectElement =
                  document.querySelector('#select1');
            output = selectElement.value;
            document.querySelector('.output').textContent = output;
        }
    </script>
</body>
 
</html>


Output:

value property

Method 2: Using the selectedIndex property

  • The selectedIndex property returns the index of the currently selected element in the dropdown list. This index starts from 0 and returns -1 if no option is selected.
  • The options property returns the collection of all the option elements in the <select> dropdown list.
  • The elements are sorted according to the source code of the page.
  • The index found before can be used with this property to get the selected element.
  • This option’s value can be found by using the value property.

Example: This example is the implementation of the above-explained approaches.

HTML




<!DOCTYPE html>
<html>
 
<head>
    <title>
        How to get selected value in
        dropdown list using JavaScript?
    </title>
</head>
 
<body>
    <h1 style="color: green">
        GeeksforGeeks
    </h1>
    <b>
        How to get selected value in
        dropdown list using JavaScript?
    </b>
    <p> Select one from the given options:
        <select id="select1">
            <option value="free">Free</option>
            <option value="basic">Basic</option>
            <option value="premium">Premium</option>
        </select>
    </p>
    <p> The value of the option selected is:
        <span class="output"></span>
    </p>
 
 
    <button onclick="getOption()">Check option</button>
    <script type="text/javascript">
 
        function getOption() {
            selectElement =
                  document.querySelector('#select1');
            output =
                  selectElement.options[selectElement.selectedIndex].value;
            document.querySelector('.output').textContent = output;
        }
    </script>
</body>
 
</html>


Output:

SelectedIndex property with option property


Whether you're preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape, GeeksforGeeks Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we've already empowered, and we're here to do the same for you. Don't miss out - check it out now!

Last Updated : 24 Nov, 2023
Like Article
Save Article
Previous
Next
Similar Reads
Complete Tutorials