Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jQuery get text with $.each from a multiple select list

I'm trying to get the text of some selected options from a select list with the jQuery.each function.

I select all selected options like this: $('#IdOfSelect option:selected')
This works just fine.

If I iterate over this jQuery-object I always get the error value.text() is not a function when I try to get the text of my single option.

You can see this behaviour here: http://jsfiddle.net/z2nbP/

In the Firebug-Console the item shows as <option value="10"> instead of my expected [option] (if it's a DOM-object).

How is it possible to iterate over those selected options and although get there texts?

like image 538
Tim Avatar asked Jun 20 '11 07:06

Tim


2 Answers

You need this:

$(value).text()

because the value parameter passed to the callback is the native DOM element which doesn't have the .text() method defined on it.

Here's the updated demo.

like image 96
Darin Dimitrov Avatar answered Oct 30 '22 17:10

Darin Dimitrov


Try this: http://jsfiddle.net/9arYE/

like image 44
silex Avatar answered Oct 30 '22 19:10

silex