Dynamically selecting Dropdown's options

I am having a dropdown. When the page loads I want to select the option based on the value.

I have a hash which contains id as key and some number as its value. I am looping through this hash and if any of the option's value matches the current loop's value I need to set that option as selected.

$.each(hash_name_here, function(key, value){
  $('select option[value=key]').attr("selected',"selected");
});

This does nothing.

But if I substitute the number instead of key it works fine.

$('select option[value=81]').attr("selected',"selected");

What am I doing wrong?


ANSWERS:


You have to let Jquery know that key is a variable. Now it's parsed as a string. Use:

$.each(hash_name_here, function(key, value){
  $('select option[value=' + key + ']').attr("selected","selected");
});

Try this

$.each(hash_name_here, function(key, value){  
  $('#key option[value=value]').prop("selected",true);
});


 MORE:


 ? jquery trigger button click, and get value of dynamic select/option
 ? Can't get value of select field in a form with jquery
 ? How to set readonly property to false on multiple textboxes with same class?
 ? How to set readonly property to false on multiple textboxes with same class?
 ? How to set readonly property to false on multiple textboxes with same class?
 ? How to set readonly property of dynamic textbox using jQuery?
 ? Textbox becomes readonly on postback even when setting the readonly attribute to false manually
 ? How to remove multiple dynamically generated elements at the same time by their class name?
 ? set readonly property to false for an html text input on clicking on Edit
 ? How to keep readonly="false" for input boxes with same class when moving focus between them?