• 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 / Get Selected Option from Dropdown Using jQuery

Get Selected Option from Dropdown Using jQuery

December 1, 2021 Leave a Comment

To get the selected option value from a dropdown using jQuery, the simplest way is with the jQuery val() method.

$("#dropdown :selected").val()

To get the selected option text from a dropdown using jQuery, the simplest way is with the jQuery text() method.

$("#dropdown :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 from the dropdown, and display the selected option’s value and the selected option’s text, we can use the jQuery val() method and jQuery text() method with the following Javascript code:

var value = $("#select-list :selected").val()
var text = $("#select-list :selected").text()

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

var value = jQuery("#select-list :selected").val()
var text = jQuery("#select-list :selected").text()

How to Get Selected Option from Dropdown using jQuery With a Click

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 read the selected option value and text from a dropdown using jQuery very easily by combining the val() and text() methods with a click event.

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 click.

<div id="div">
    <p id="click-me">Click Me to Show The Selected Option's Value and Text</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 click() method

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

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

$("#click-me").click(function(){
    $("#selected-value").text($("#select-list :selected").val())
    $("#selected-text").text($("#select-list :selected").text())
}); 

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

Code Output:

Click Me to Show The Selected Option’s Value and Text

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

Full Code:


<div class="html-code-output">
<div id="div">
    <p id="click-me">Click Me to Show The Selected Option's Value and Text</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>

$("#click-me").click(function(){
    $("#selected-value").text($("#select-list :selected").val())
    $("#selected-text").text($("#select-list :selected").text())
});

</script>

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

Other Articles You'll Also Like:

  • 1.  How to Set Checkbox Checked With jQuery
  • 2.  Using jQuery to Show a Div
  • 3.  Using jQuery to Show and Hide a Div
  • 4.  jQuery Display None
  • 5.  How to Use jQuery to Remove Element
  • 6.  Using the jQuery fadeTo Method to Change the Opacity of an Element
  • 7.  How to Use jQuery to Clear a textarea Field
  • 8.  How to Clear an Input Field Using jQuery
  • 9.  Using jQuery to Hide Element by Class
  • 10.  Using jQuery to Remove Attribute from HTML Element

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