Like val (), but for selected text in select multiple?

Possible duplicate:
jQuery get selected text from dropdown

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

Using jquery, I can get the value of the selected elements as follows:

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

But surprisingly, when I try to get the actual selected text (for example, transaction 1), it gives me two entries:

 var selected_text = $(e.target).text(); >> "\ndeal 2\ndeal 1\n" 

Why is this and how can I get the text of the selected entries?

+4
source share
2 answers

The jQuery method "text ()" returns the inner text of the selected element. In your case, you select the entire select tag, so it gives you all the text inside it (excluding the nested tags themselves). Use instead:

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

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

+4
source

e.Target contains a selection list! This is why you get the text of both options when you call text() . If you call val() , you get the value of the select list, which is the value of one of the parameters.

0
source

All Articles