How can I use jQuery to move a specific <option> to be the second option?
<select class="comestibles">
<option>apple</option>
<option>banana</option>
<option>cantaloupe</option>
<option>date</option>
<option>eggplant</option>
<option>fig</option>
</select>
<select class="comestibles">
<option>apple</option>
<option>banana</option>
<option>cantaloupe</option>
<option>date</option>
<option>eggplant</option>
<option>fig</option>
</select>
This code will make eggplant the first option… close but no cigar.
jQuery('select.comestibles').each(function(){
$('option[value=eggplant]',this).prependTo(this);
});
Assuming there’s only one select element…
I also assume that you actually have
valueattributes on your elements.http://jsfiddle.net/wNmLF/
The code in the question changed. Now it would be…
For the most recent change…