1

I have this HTML:

<select class="business_types_select" name="business_type_id">
<option value="0" selected="selected">Select a Business Type</option>
     <option value="54">Bakery</option>
      <option value="55">Tobacco</option>
</select>

and if I select Bakery and then try:

 $(".business_types_select").val()

I get 54, but how do i get the text Bakery? If I try

 $(".business_types_select").text()

I get:

" Select a Business Type Bakery Tobacco "
Nic
  • 13,287
  • 7
  • 40
  • 42
Matt Elhotiby
  • 43,028
  • 85
  • 218
  • 321
  • possible duplicate of [jquery get selected text from dropdownlist](http://stackoverflow.com/questions/1643227/jquery-get-selected-text-from-dropdownlist) – Naftali May 05 '11 at 18:19

5 Answers5

11

Try:

$(".business_types_select :selected").text()

Without this, the .text() is behaving like it should, which is to bring back all of the text from that particular class. Adding :selected to the selector narrows it down to just the one you have selected.

Remember to cache your selectors if you're going to be operating on them often for performance.

Nic
  • 13,287
  • 7
  • 40
  • 42
4

You have to reduce the selector to the selection option:

$(".business_types_select").find('option:selected').text();
mVChr
  • 49,587
  • 11
  • 107
  • 104
2

Like this:

$(".business_types_select > option:selected").text()

(In the short example here I have assumed that there is only one <select class="business_types_select" />. My jsfiddle example is more comprehensive.)

Lightness Races in Orbit
  • 378,754
  • 76
  • 643
  • 1,055
1
$(".business_types_select option:selected").text()

That will return the text of the selected option only

Chad
  • 19,219
  • 4
  • 50
  • 73
0
$(".business_types_select").find('option:selected').text();
davidosomething
  • 3,379
  • 1
  • 26
  • 33