W3docs

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

Read the tutorial and learn the methods of getting the value of the selected option in a select box using jQuery. Read about the text() and val() methods.

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 get the text or the value of the selected option by using the :selected pseudo-class or the <kbd class="highlighted">val()</kbd> method in jQuery.

The <kbd class="highlighted">val()</kbd> method returns the value of the selected option.

jQuery returns the value of the selected option

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

The <kbd class="highlighted">text()</kbd> method returns the text content of the selected option.

jQuery returns the text of the selected option

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

Let's see how these two methods work with the following example:

jQuery select 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 <kbd class="highlighted">val()</kbd> method returns the value of the option (for example, "1"), and if you want to get the text "Tom", you can use the <kbd class="highlighted">text()</kbd> method.

The text() and val() Methods

The <kbd class="highlighted">text()</kbd> 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 <kbd class="highlighted">val()</kbd> 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:

How to get the value of the selected option in a select box using JavaScript?

<!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");
  selectBox.addEventListener("change", function() {
    var selectedValue = selectBox.value;
    alert(selectedValue);
  });
</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 displayed it in an alert box.

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");
selectBox.addEventListener("change", function() {
  var selectedOption = selectBox.options[selectBox.selectedIndex];
  var selectedText = selectedOption.textContent;
  alert(selectedText);
});

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:

How to get the text of the selected option via JavaScript?

<!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");
selectBox.addEventListener("change", function() {
  var selectedOption = selectBox.options[selectBox.selectedIndex];
  var selectedText = selectedOption.textContent;
  alert(selectedText);
});
</script>
  </body>
</html>