views:

94

answers:

1

Hi all,

I have a select box with following options

select id="box" ><br/>
  option value="1"Same Day<br/>
  option value="5">96<br/>
  option value="4">72<br/>
  option value="3">48<br/>
  option value="2">24<br/>
select>

I Want to add attr selected to option value '4' , i know i can show it selected via

$("#box").val(4)

But this just shows it selected but does not add attribute selected to option 4, what i want is to add attribute selected to option.

Please suggest

Thanks

+1  A: 
$('#box').children('option:eq(3)').attr('selected', 'selected');

That makes usage of the eq() selector which querys the index you specify.

Slightly faster (even if it's more typing) would be the method .eq() instead of the selector:

$('#box').children('option').eq(3).attr('selected', 'selected');

Since .eq() uses array slices instead of query'ing over Sizzle. As you notice correctly, both versions start indexing at zero.

Ref.: :eq, .eq()

jAndy
Hi Andy,Soorry but forgot to tell you that select box is dyanamic so i dont know where the option with value 4 will come everytime.I want to trac it using option with value 4.But thanks for great help and even more appreciated if you can get me out of this situtation. :)
Jos