4

Possible Duplicate:
jQuery get select option text

I have a drop downlist as like:

<select id="systemMessageList" name="systemMessageList">
    <option value="-1">--- Choose One ---</option>  
    <option value="120">niempo</option>  
    <option value="119">quartero</option>  
    <option value="118">mileno</option>  
</select>

I want that: I will pass the value for example 119 and it will return the text quartero in JQuery?

Community
  • 1
  • 1
kamaci
  • 72,915
  • 69
  • 228
  • 366

4 Answers4

5

For the text of the selected option, you need:

$('#systemMessageList option:selected').text();

For the value you can just use

$('#systemMessageList').val();

Edit: the other answers are probably what you want. I'm just showing you how to get the text and value of whichever option is selected.

Calum
  • 5,308
  • 1
  • 22
  • 27
4

try this:

$('option[value="'+$('#systemMessageList').val()+'"]').text();

so if 119 is selected it will return: quartero

or you can create some sort of function that gets the text for any option value:

function getOptionText(option){
    return $('option[value="'+option+'"]').text();
}

getOptionText(119); // yields quartero
Naftali
  • 144,921
  • 39
  • 244
  • 303
3
var text = $('#systemMessageList option[value="119"]').text();
alex
  • 479,566
  • 201
  • 878
  • 984
1

Try this:

function getOptionText(value){
    return $('#systemMessageList option[value="' + value + '"]').text(); 
}

alert(getOptionText(119));
Chandu
  • 81,493
  • 19
  • 133
  • 134