Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Similar to val() but for the selected text in a select multiple? [duplicate]

Tags:

jquery

Possible Duplicate:
jQuery get selected text from dropdownlist

<select id="id_deals" name="deals" multiple="multiple">
  <option value="1">deal 2</option>
  <option value="2">deal 1</option>
</select>

With jquery I can get the value of the selected items like this:

var selected = $(e.target).val();

>> 2

But surprisingly when I try to get the actual selected text (e.g. deal 1), it gives me both entries:

var selected_text = $(e.target).text();

>> "\ndeal 2\ndeal 1\n"

Why is that and how could I get the text of the selected entries as well?

like image 347
Houman Avatar asked Jul 20 '12 15:07

Houman


1 Answers

The jQuery "text()" method returns the inner text of the selected element. In your case you're selecting the whole select tag, so it's giving you all the text inside it (excluding the nested tags themselves). Instead, use:

$("#yourdropdownid option:selected").text();

See this: Get selected text from a drop-down list (select box) using jQuery

like image 106
greg84 Avatar answered Oct 03 '22 21:10

greg84