Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get text of the selected option with jQuery [duplicate]

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.

like image 968
dotty Avatar asked Jun 23 '11 12:06

dotty


People also ask

How do I get the text value of a selected option?

find(":selected"). text(); it returns the text of the selected option.

How do I get the selected value and current selected text of a dropdown box using jquery?

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.

What is select in Javascript?

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.


2 Answers

Close, you can use

$('#select_2 option:selected').html() 
like image 109
JohnP Avatar answered Oct 03 '22 06:10

JohnP


$(document).ready(function() {     $('select#select_2').change(function() {         var selectedText = $(this).find('option:selected').text();         alert(selectedText);     }); }); 

Fiddle

like image 35
bancer Avatar answered Oct 03 '22 07:10

bancer