How to Get the Value of Selected Option in a Select Box

In this tutorial, you will learn how to get the value of the selected option in the select box using jQuery. There are two methods of getting the value of the selected option. You can either select text or find the position of it in a drop-down list by using the option:selected attribute or the val() method in jQuery.

The val() method returns the value of selected attribute value.

let selectedValue = $("#selectVal option:selected").val();

The text() method returns a string of the selected option.

let selectedValue = $("selectVal option:selected").text();

Let's show what will return these two methods with the following example:

<!DOCTYPE html>
<html>
  <head>
    <title>Title of the Document</title>
    <script src="https://code.jquery.com/jquery-3.5.0.min.js"></script>
  </head>

  <body>
    <form>
      <label>Select Names:</label>
      <select class="selectVal">
        <option value="1">Tom</option>
        <option value="2">John</option>
        <option value="3">James</option>
        <option value="4">Ann</option>
        <option value="5">Maria</option>
      </select>
    </form>
    <script>
      $(document).ready(function() {
          $("select.selectVal").change(function() {
              let selectedItem = $(this).children("option:selected").val();
              alert("You have selected the name - " + selectedItem);
            });
        });
    </script>
  </body>
</html>

The val() method will select the value of the option for example "1", and if you want to get the string "Tom" you can use the text() method.

The text() and val() Methods

The text() method will set or return the text content of the selected elements.

It returns the text content of all matched elements. When it is used to set content, it overwrites the content of all matched elements.

The val() method is an inbuilt method in jQuery, which is used to get the values of form elements (e.g., input, select, textarea). It will return the value of the value attribute of the first matched element.

Using Vanilla JavaScript

To get the value of the selected option in a select box using JavaScript, you can use the value property of the select element. Here's an example:

<!DOCTYPE html>
<html>
  <head>
    <title>Title of the Document</title>
    <script src="https://code.jquery.com/jquery-3.5.0.min.js"></script>
  </head>

  <body>

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

<script>
  var selectBox = document.getElementById("my-select");
  var selectedValue = selectBox.value;
  alert(selectedValue); // prints the value of the selected option to the console
</script>
  </body>
</html>

In this example, we have a select box with three options, each with a different value. We've used JavaScript to get a reference to the select box using its ID, my-select. We've then used the value property of the select box to get the value of the currently selected option. We've stored this value in a variable called selectedValue and logged it to the console.

Note that if you want to get the text of the selected option instead of its value, you can use the textContent property of the selected option element. Here's an example:

var selectBox = document.getElementById("my-select");
var selectedOption = selectBox.options[selectBox.selectedIndex];
var selectedText = selectedOption.textContent;
alert(selectedText); // alerts the text of the selected option to the console

In this example, we've used the selectedIndex property of the select box to get the index of the currently selected option. We've then used this index to get a reference to the selected option element using the options property of the select box. We've used the textContent property of the selected option to get its text content and stored it in a variable called selectedText.

And here's the full example:
<!DOCTYPE html>
<html>
  <head>
    <title>Title of the Document</title>
    <script src="https://code.jquery.com/jquery-3.5.0.min.js"></script>
  </head>

  <body>

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

<script>
var selectBox = document.getElementById("my-select");
var selectedOption = selectBox.options[selectBox.selectedIndex];
var selectedText = selectedOption.textContent;
alert(selectedText); // alerts the text of the selected option to the console
</script>
  </body>
</html>