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!

1

2 Answers 2

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/

Sign up to request clarification or add additional context in comments.

1 Comment

works like a charm! thanks! (need to wait before I give you green.)
1

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.