Follow-up to this question: Setting background-color of select options in JQuery
I have a page with multiple select boxes, as per the following:
<select name="item-0-status" id="id_item-0-status">
<option value="">---------</option>
<option value="1">Online</option>
<option value="2">Offline</option>
<option value="3">Unknown</option>
</select>
These are being auto-generated in django, so it is not possible to apply css classes, ids or attributes directly to the options. The select elements have ids of 'item-0-status','item-1-status','item-2-status' etc.
I am allocating colours to the options via the following JQuery code:
$('select[id$=-status][id^=id_item-]').children().each(
function (){
if($(this).val() == 0){
$(this).css('backgroundColor','white');
}
if($(this).val() == 1){
$(this).css('backgroundColor','green');
}
if($(this).val() == 2){
$(this).css('backgroundColor','red');
}
if($(this).val() == 3){
$(this).css('backgroundColor','orange');
}
}
);
Which works fine.
I also want the select elements to have the same background colour as the selected option, which I am trying to achieve using the following:
$('select[id$=-status][id^=id_item-]').each(
function (){
$(this).css('backgroundColor',$('option:selected',this).css('backgroundColor'));
}
);
However, this just colours the select element in blue (I think it is taking the colour from the hover property rather than the background). This is in Firefox 3.6.8, which for the purposes of this question is the only browser concerned.
Any idea how to fix this?