Jens Rentmeesters
Jens Rentmeesters

Reputation: 55

Jquery: get the text of the selected option in this

If the input is a select type, I want to get the text of the selected option, not the value.

This is what I have so far:

$('.encodageField').live('change', function(){
  if( $(this).is('select') ) {
      val = $($(this) + 'option:selected').text();
      alert (val);
  }else{
      val = $(this).val;
      alert (val);
  }
  save_answer($(this));
});

So I need the text of $(this) the changed select. How do I use 'this' in combination with 'option:selected' as a selector.

Upvotes: 1

Views: 145

Answers (2)

Johan
Johan

Reputation: 35213

I think this should be equivalent to Leigh's answer

$(this).find('option:selected').text();

Upvotes: 0

Leigh Ciechanowski
Leigh Ciechanowski

Reputation: 1317

try this

val = $('option:selected', this).text();

Upvotes: 3

Related Questions