views:

52

answers:

2

I have an HTML combobox which contains unique options. For example, a country dropdown which contains country_id as a value and country name as text.

Is there any way using jQuery or JavaScript to let me set the selected item using the country name not the country_id?

+1  A: 
$("option:contains('COUNTRY NAME HERE')").attr('selected','selected');

something like that?

Jamie Wong
no valid xhtml! it would be `.attr('selected', 'selected');`
Andreas Niedermair
@Andreas Thanks. Fixed.
Jamie Wong
@jamie: you're welcome!
Andreas Niedermair
dude it was really great and it works perfect but I am greaty instead of contains is there any other key word match the word as its coz I have some names similar to each other
A: 

You can do write like below:

$("option:selected").text() 

to obtain the text given for the option

Jenn
-, this is for retrieving not for setting
Andreas Niedermair