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.
Đang xem: How do i get the text value of a selected option?
$('#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:
Option 0 Option 1 Option 2
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.
Select an option to show the value and text of that option.
Option 0 Option 1 Option 2 Option 3
The selected option's value is: , and the selected option's text is .
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.
Xem thêm: Có Nên Uống Thuốc Cắt Sữa Mẹ, Thuốc Tiêu Sữa Có An Toàn Không
Option 0Option 1Option 2Option 3
The selected option’s value is: , and the selected option’s text is .
Full Code:
Select an option to show the value and text of that option.
Option 0 Option 1 Option 2 Option 3
The selected option's value is: , and the selected option's text is .
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:
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 *