views:

1037

answers:

3

Is there a style for a select option's "selected" color? For example:

<HTML>
<BODY>
<FORM NAME="form1">
<SELECT NAME="mySelect" SIZE="7" style="background-color:red;">
<OPTION>Test 1
<OPTION>Test 2
<OPTION>Test 3
<OPTION>Test 4
<OPTION>Test 5
<OPTION>Test 6
<OPTION>Test 7
</SELECT>
</FORM>
</BODY>
</HTML>

When I select an option it turns blue, I want to override this and make it a different color. In the style I expected something like "selected-color", but it doesn't exist.

+1  A: 

Currently CSS does not support this feature.

You can build your own or use a plug-in that emulates this behaviour using DIVs/CSS.

Diodeus
+1  A: 
You may not be able to do this using pure CSS. But, a little javascript can do it nicely.

A crude way to do it -

var sel = document.getElementById('select_id');
sel.addEventListener('click', function(el){
    var options = this.children;
    for(var i=0; i < this.childElementCount; i++){
        options[i].style.color = 'white';
    }
    var selected = this.children[this.selectedIndex];
        selected.style.color = 'red';
    }, false);
N 1.1
+1 if you can tell us how.
J.Hendrix
ok let me try :)
N 1.1
N 1.1
Thanks, I'll have a go to some jQuery and get it done. Could post. luv
arieltools
@arieltools: ya. will be easy with jquery.
N 1.1
+1  A: 

You cannot rely on CSS for form elements. The results vary wildly across all the browsers. I don't think Safari lets you customize any form elements at all.

Your best bet is to use a plugin like jqTransform (uses jQuery).

EDIT: that page doesn't seem to be working at the moment. There is also Custom Form Elements which supports MooTools and may support jQuery in the future.

DisgruntledGoat
Yeah, would have to to that if there's no go with jQuery. Cheers.
arieltools
@ari: there are quite a few of these plugins around - I just saw two new ones today: http://swizec.com/code/styledButton/ and http://pixelmatrixdesign.com/uniform/
DisgruntledGoat