0

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...

PSR
  • 39,804
  • 41
  • 111
  • 151

3 Answers3

1

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());
});
dsgriffin
  • 66,495
  • 17
  • 137
  • 137
0

See .className is a collection itself so you can use it this way on the event of change get the option:selected of it and then alert text of it.

$('.className').on('change', function(){
   alert($("option:selected", this).text());
});
Jai
  • 74,255
  • 12
  • 74
  • 103
0

Please use this

 $('.className').each(function(index,value){
    if($(this).is(':checked'))
      { alert($(this).val());}
  });
Deniyal Tandel
  • 512
  • 3
  • 14