views:

415

answers:

3

I have a dropdown as:

<select id="HowYouKnow" >
  <option value="1">FRIEND</option>
  <option value="2">GOOGLE</option>
  <option value="3">AGENT</option></select>

In the above dropdown i know the text of the dropdown. How can set the value of the dropdown in document.ready with the text using jquery?

A: 
$("#HowYouKnow").val("GOOGLE");
dell
$(document).ready(function() { $("#HowYouKnow").val("GOOGLE"); });Sets the value too... not sure why I got down voted!
dell
+1  A: 
$("#HowYouKnow option:eq(XXX)").attr('selected', 'selected');

where XXX is the index of the one you want.

contagious
How can i get the index of the text?
Prasad
See my answer...tested and working.
Peter J
+1  A: 

This is a method that works based on the text of the option, not the index. Just tested.

var theText = "GOOGLE";
$("#HowYouKnow option:contains(" + theText + ")").attr('selected', 'selected');
Peter J
it worked great
Prasad