• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar

The Programming Expert

Solving All of Your Programming Headaches

  • HTML
  • JavaScript
  • jQuery
  • PHP
  • Python
  • SAS
  • Ruby
  • About
You are here: Home / jQuery / Using jQuery to Get Value of Select On Change

Using jQuery to Get Value of Select On Change

December 10, 2021 Leave a Comment

To get the selected option value on change from a dropdown using jQuery, the simplest way is to use the jQuery change method, jQuery find() method, and jQuery val() method.

$('#dropdown').on('change', function() {
  var selected_option_value = $(this).find(":selected").val();
});

To get the selected option text on change from a dropdown using jQuery, the simplest way is to use the jQuery change method, jQuery find() method, and jQuery text() method.

$('#dropdown').on('change', function() {
  var selected_option_value = $(this).find(":selected").text();
});

Let’s say I have the following HTML:

<div id="div">
    <select id="select-list">
        <option value="0">Option 0</option>
        <option value="1">Option 1</option>
        <option value="2">Option 2</option>
    </select>
</div>

To get the selected option on change from the dropdown, and display the selected option’s value and the selected option’s text, we can use the jQuery change() method combined with the val() method and jQuery text() method with the following Javascript code:

$('#select-list').on('change', function() {
  var selected_option_value = $(this).find(":selected").val();
  var selected_option_text = $(this).find(":selected").text();
});

If you are using WordPress, don’t forget to change the $ to jQuery as below:

jQuery('#select-list').on('change', function() {
  var selected_option_value = jQuery(this).find(":selected").val();
  var selected_option_text = jQuery(this).find(":selected").text();
});

How to Get Selected Option On Change using jQuery

Many times when creating a web page and the user experience, we want to read the user inputs when a user interacts with another element on the web page.

We can get the selected option value on change from a dropdown using jQuery very easily by combining the change() method with the val() and text() methods.

Let’s say we have the following HTML code and we want to show the selected option’s value and text in a paragraph. We will change the text in the spans below on change.

<div id="div">
    <p>Select an option to show the value and text of that option.</p>
    <select id="select-list">
        <option value="0">Option 0</option>
        <option value="1">Option 1</option>
        <option value="2">Option 2</option>
        <option value="3">Option 3</option>
    </select>
    <p>The selected option's value is: <span id="selected-value"></span>, and the selected option's text is <span id="selected-text"></span>.</p>
</div>

We can utilize the jQuery change() method

, jQuery val() method, and jQuery text() method to get the text of the selected option, and value of the selected option on change.

Below is the Javascript code which will allow the user to be able to get the selected optionusing jQuery:

$('#select-list').on('change', function() {
  $("#selected-value").text($(this).find(":selected").val());
  $("#selected-text").text($(this).find(":selected").text());
});

The final code and output for this example of how to get the selected option from a dropdown on change using jQuery and Javascript is below:

Code Output:

Select an option to show the value and text of that option.

The selected option’s value is: , and the selected option’s text is .

Full Code:


<div class="html-code-output">
<div id="div">
    <p>Select an option to show the value and text of that option.</p>
    <select id="select-list">
        <option value="0">Option 0</option>
        <option value="1">Option 1</option>
        <option value="2">Option 2</option>
        <option value="3">Option 3</option>
    </select>
    <p>The selected option's value is: <span id="selected-value"></span>, and the selected option's text is <span id="selected-text"></span>.</p>
</div>
</div>

<script>

$('#select-list').on('change', function() {
  $("#selected-value").text($(this).find(":selected").val());
  $("#selected-text").text($(this).find(":selected").text());
});

</script>

Hopefully this article has been useful for you to understand how to use jQuery to get the selected value on change from a dropdown.

Other Articles You'll Also Like:

  • 1.  How to Set Checkbox Checked With jQuery
  • 2.  jQuery Display None
  • 3.  Using jQuery to Set a Radio Button to Checked
  • 4.  Using jQuery to Wait 1 Second Before Executing Code
  • 5.  Using jQuery to Get the Value of a Radio Button
  • 6.  Using jQuery to Move Element After Another
  • 7.  Using jQuery to Select Multiple Ids
  • 8.  Using jQuery to Check if Element Has Class
  • 9.  Add Style to HTML Element Using jQuery
  • 10.  jQuery get first child

About The Programming Expert

The Programming Expert is a compilation of a programmer’s findings in the world of software development, website creation, and automation of processes.

Programming allows us to create amazing applications which make our work more efficient, repeatable and accurate.

At the end of the day, we want to be able to just push a button and let the code do it’s magic.

You can read more about us on our about page.

Reader Interactions

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Primary Sidebar

About The Programming Expert

the programming expert main image

Welcome to The Programming Expert. We are a group of US-based programming professionals who have helped companies build, maintain, and improve everything from simple websites to large-scale projects.

We built The Programming Expert to help you solve your programming problems with useful coding methods and functions in various programming languages.

Search

Learn Coding from Experts on Udemy

Looking to boost your skills and learn how to become a programming expert?

Check out the links below to view Udemy courses for learning to program in the following languages:

Copyright © 2023 · The Programming Expert · About · Privacy Policy