3

I am trying to get the user selected texts for my dropdown menu.

I have

   var selectMenu=document.createElement('select');
            selectMenu.className='menu';

        for(var i=0; i<array.length; i++){

           var option=document.createElement('option');
               option.className='option';
               option.innerHTML=array[i].name;
               option.value=array[i].id;

            selectMenu.appendChild(option);
         }

         $(selectMenu).change(function(){

           //i want to get the selected text here

           //I know I could get value by using $(this).val()
           //but not sure how to get the selected text here.

         })

I have google the issue and all I found are like

$('#menu option:selected).text().

Are there anyways to get what I need? Thanks a lot!

FlyingCat
  • 14,036
  • 36
  • 119
  • 198
  • http://stackoverflow.com/questions/1643227/get-selected-text-from-dropdownlist-using-jquery – Radi Jan 04 '13 at 21:33

2 Answers2

7

if you have something like

<select>
    <option value='1'> SO</option>
    <option value='2'>GOOGLE</option>
</select>

you can try

$("select").change(function(e){
    console.log($(":selected",this).text());
});

http://jsfiddle.net/Ykzp7/

Rafay
  • 30,950
  • 5
  • 68
  • 101
1

try $('.menu option:selected).text() instead of $('#menu ...

obs
  • 797
  • 3
  • 14
  • 37