header image
 

jQuery – programmatically selecting a value in a dropdown

Assume i have a dropdown called cmbCountry, with option values corresponding to 2 char country codes

I tried the much publicized soln of  $(“#cmbCountry”).val(sCountryCode);
nothing happened :( , turns out it was doing a weird sort of multiselect.

what do i do?

well after much trial and error, the solution is as follows

$(“#cmbCountry option”).attr(“selected”,”false”);
and then
$(“#cmbCountry”).val(sCountryCode);

and hey presto! it worked!!!

so the things to do are (1) clear the selected attribute of every option, (2) use the val() property to set the selected item

~ by shoel on March 4, 2010 .



Leave a Reply