views:

95472

answers:

12

Using jQuery, how do you check if there is an option selected in a select menu, and if not, assign one of the options as selected.

(The select is generated with a maze of PHP functions in an app I just inherited, so this is a quick fix while I get my head around those :)

+2  A: 

Look at the selectedIndex of the select element. BTW, that's a plain ol' DOM thing, not JQuery-specific.

Hank Gay
+3  A: 

No need to use jQuery for this:

var foo = document.getElementById('yourSelect');
if (foo)
{
   if (foo.selectedIndex != null)
   {
       foo.selectedIndex = 0;
   } 
}
FlySwat
The OP stated, "Using jQuery ..." so it seems superfluous to ignore that, no?
BryanH
+54  A: 

While I'm not sure about exactly what you want to accomplish, this bit of code worked for me.

<select id="mySelect" multiple="multiple">
  <option value="1">First</option>
  <option value="2">Second</option>
  <option value="3">Third</option>
  <option value="4">Fourth</option>
</select>

<script type="text/javascript"> 
$(document).ready(function() {
  if (!$("#mySelect option:selected").length)
    $("#mySelect option[value='3']").attr('selected', 'selected');
});
</script>
Joe Lencioni
The selection would be easier to read like this: `$("#mySelect").val(3);`
Jørn Schou-Rode
This got me on the right track, but I needed to do this:`$("#mySelect option")[2]['selected'] = true;`Figured that out after investigating the object structures in Firebug.
semperos
+6  A: 

lencioni's answer is what I'd recommend. You can change the selector for the option ('#mySelect option:last') to select the option with a specific value using "#mySelect option[value='yourDefaultValue']". More on selectors.

If you're working extensively with select lists on the client check out this plugin: http://www.texotela.co.uk/code/jquery/select/. Take a look the source if you want to see some more examples of working with select lists.

Alex
+1  A: 

Thanks, lencioni's code looks like a good way to do it.

In the meantime I already came across the texotela plugin mentioned, which let me solve it like this:

$(document).ready(function(){
    if ( $("#context").selectedValues() == false) {
    $("#context").selectOptions("71");
    }
});
meleyal
+2  A: 

Here is my function changing the selected option. It works for jQuery 1.3.2

function selectOption(select_id, option_val) {
    $('#'+select_id+' option:selected').removeAttr('selected');
    $('#'+select_id+' option[value='+option_val+']').attr('selected','selected'); 
}
sata
A: 

This was a quick script I found that worked... .Result is assigned to a label.

$(".Result").html($("option:selected").text());

Joel
A: 

You guys are doing way too much for selecting. Just select by value:

$("#mySelect").val( 3 );
giagejoe
This is selecting option 3 regardless if another option is already selected.
Jordan Ryan Moore
+1  A: 

Easy! The default should be the first option. Done! That would lead you to unobtrusive JavaScript, because JavaScript isn't needed :)

Unobtrusive JavaScript

Labuschin
+3  A: 
$(document).ready(function() { if (!$("#mySelect option:selected").length) $("#mySelect").val( 3 ); });
Ram Prasad
A: 
alert("a");

teste

teste
A: 

$("option[value*='2']").attr('selected', 'selected');// 2 for example, add * for every option