In this tutorial we will see how to Get Selected Option Text onchange with jQuery. The jQuery text() method and change() method can be used to get the text of the selected option when the option is changed.
Table of Contents
jQuery text() Method
jQuery text() Method is used to set or get the text content of the selected HTML elements.
In this example text() Method is used to read the text of the selected option of the select tag.
jQuery change() Method
jQuery change() Method triggers the change event or attaches the function with the change event. Thus function will be executed when change event occurs.
In this example change() Method will execute the function as soon as the option of select tag is changed and new option is selected.
HTML Code
HTML Code is given below, in this code we have a select tag with three options. Each option has it's own unique text.
<select name="lang" id="lang">
<option value="html">HTML</option>
<option value="css">CSS</option>
<option value="jquery">jQuery</option>
</select>
JQuery Code
JQuery Code is given below, In this code the text of selected option is read on change event. This text is read using id of select tag and the :selected Selector.
:selected Selector only targets the option which is selected.
Take a look at the code given below.
<script>
$(document).ready(function(){
$('#lang').change(function(){
var text= $("#lang option:selected").text();
alert(text);
});
});
</script>
Demo
Video Tutorial
Watch video tutorial on How To Get Selected Option Text onchange using jQuery.