Search code examples
jqueryjquery-selectbox

how to get the selected text value using this in jQuery


I need to get the selected option text in jQuery of a select box.I used

   $('#selectId :selected').text();

It is working fine for me.But how i can do this with following.Here i am iterating all select boxes which have a particular class.I used following code.But it is not working for me.

 $('.className').each(function(index,value){
    alert($(this :selected).text());
  });

It is giving the error SyntaxError: missing ) after argument list

Please help me .Thanks in advance...


Solution

  • That's incorrect syntax. You could use:

    $('.className').each(function(index,value){
        alert($(this).find('option:selected').text());
    });
    

    or as an alternative:

    $('.className').each(function(index,value){
        alert($("option:selected", this).text());
    });