views:

73

answers:

3

Hi,

I am wanting to automatically select the option with the value of fr.

<select class="goog-te-combo">

<option value="">Select Language</option>
<option value="fr">French</option>

</select>

Is this possible with javascript or jQuery ?

+1  A: 

Without jQuery you need brute force

var country="fr";
//If you know the form and name of the select:
var sel = document.forms[0].sel1;

//If you know the id of the select:
var sel = document.getElementById('sel1');

// If you only know the className:
var sels = document.getElementsByTagName('select');
for (var i=0,n=sels.length;i<n;i++) {
  if (sels[i].className=="goog-te-combo") { // assume multiple selects with same class
    for (var j=0, m=sels[i].options.length;j<m;j++) {
      if (sels[i].options[j].value==country) sels[i].options[j].selected=true;
    }
  }
}
mplungjan
+2  A: 

This sets the French option:

$('.goog-te-combo').val("fr");
Gert G
A: 

The following snippet will also set the fr option:

$('option[value="fr"]').attr({selected: 'true'});
Rob