0

I have a select control that looks like this:

<select name="MyName" id="MyID">
  <option value ="1">1 day</option>
  <option value ="2">2 days</option>
  <option value ="3">3 days</option>
  <option value ="4">4 days</option>
<select>

I'm looking to get the text of the option based on the value. For instance, if I input 2, it should return "2 days".

Thanks for your suggestions.

frenchie
  • 51,731
  • 109
  • 304
  • 510
  • 1
    possible duplicate of [jQuery get select option text](http://stackoverflow.com/questions/196684/jquery-get-select-option-text) | There are already lot of similar questions. Just search for it. – Felix Kling Apr 27 '11 at 17:10

3 Answers3

1

try this out:

$('#MyID').change(function(){

   alert($("option[value="+this.value+"]", this).text()); 
     //added so it only finds options in this select element  

})

fiddle: http://jsfiddle.net/maniator/bzscs/

Naftali
  • 144,921
  • 39
  • 244
  • 303
1
 alert($('option[value=2]', $('#MyID')).html());
Frenchi In LA
  • 3,139
  • 3
  • 25
  • 41
  • ok, this works. Made a small tweak: alert($('option[value= ' + MyVal + ']', $('#MyID')).html()); so that I can put MyVal as a paramater. Thanks – frenchie Apr 27 '11 at 17:22
  • This is a very strange way. Much easier: `alert($('#MyID option[value=2]').text())` as already shown in the question I linked to. Why serialize the DOM? – Felix Kling Apr 27 '11 at 17:28
0

Another could be:

$('select').change(function(){
    alert($('option[value = "' + $(this).val + '"]', this).text());
});

If you have more than one select.

Jepser Bernardino
  • 1,331
  • 1
  • 12
  • 22