I have a select box with some values. How do I get the selected options text, not the value?
<select name="options[2]" id="select_2"> <option value="">-- Please Select --</option> <option value="6" price="0">100</option> <option value="7" price="0">125</option> <option value="8" price="0">150</option> <option value="9" price="0">175</option> <option value="10" price="0">200</option> <option value="3" price="0">25</option> <option value="4" price="0">50</option> <option value="5" price="0">75</option> </select>
I tried this:
$j(document).ready(function(){ $j("select#select_2").change(function(){ val = $j("select#select_2:selected").text(); alert(val); }); });
But it's coming back with undefined
.
find(":selected"). text(); it returns the text of the selected option.
We can select text or we can also find the position of a text in a drop down list using option:selected attribute or by using val() method in jQuery. By using val() method : The val() method is an inbuilt method in jQuery which is used to return or set the value of attributes for the selected elements.
Introduction to the HTML select elementsA <select> element provides you with a list of options. A <select> element allows you to select one or multiple options. To create a <select> element, you use the <select> and <option> elements.
Close, you can use
$('#select_2 option:selected').html()
$(document).ready(function() { $('select#select_2').change(function() { var selectedText = $(this).find('option:selected').text(); alert(selectedText); }); });
Fiddle
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With