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:
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.
Click Me to Show The Selected Option's Value and Text
The selected option's value is: , and the selected option's text is .
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:
Click Me to Show The Selected Option's Value and Text
The selected option's value is: , and the selected option's text is .
<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.