tags:

views:

7628

answers:

4

If you know the Index, Value or Text. also if you don't have an ID for a direct reference.

This, this and this are all helpful answers.

Example markup

<div class="selDiv">
 <select class="opts">
  <option selected value="DEFAULT">Default</option>
  <option value="SEL1">Selection 1</option>
  <option value="SEL2">Selection 2</option>
 </select>
</div>
+1  A: 

Answering my own question for documentation. I'm sure there are other ways to accomplish this, but this works and this code is tested.

<html>
<head>
<script language="Javascript" src="javascript/jquery-1.2.6.min.js"></script>
<script type="text/JavaScript">

$(function() {
 $(".update").bind("click",  // bind the click event to a div
  function() {
   var selectOption = $('.selDiv').children('.opts') ;
   var _this = $(this).next().children(".opts") ;

   $(selectOption).find("option[index='0']").attr("selected","selected");
//   $(selectOption).find("option[value='DEFAULT']").attr("selected","selected");
//   $(selectOption).find("option[text='Default']").attr("selected","selected");


//   $(_this).find("option[value='DEFAULT']").attr("selected","selected");
//   $(_this).find("option[text='Default']").attr("selected","selected");
//   $(_this).find("option[index='0']").attr("selected","selected");

 }); // END Bind
}); // End eventlistener

</script>
</head>
<body>
<div class="update" style="height:50px; color:blue; cursor:pointer;">Update</div>
<div class="selDiv">
  <select class="opts">
   <option selected value="DEFAULT">Default</option>
   <option value="SEL1">Selection 1</option>
   <option value="SEL2">Selection 2</option>
  </select>
 </div>
</body>
</html>
CarolinaJay65
A: 

If you want to select an option by value:

 jQuery('#ddl').attr('value', aValue)

Mike

Mike
+4  A: 

You can just use val() method:

$('select').val('the_value');
Leandro Ardissone
A: 

If you want to select an option by value:

jQuery('.opts').attr('value', aValue)

Atul K.