views:

278

answers:

3

Hi is it possible and how to change background color for an item in a drop down list on focus using css or javascript?

Thanks

A: 

If the native lists don't work for you, you can simulate a drop down list with JavaScript and an absolutely positioned DIV and style that. Most UI frameworks have extensive lists. Check out YUI, for example.

Aaron Digulla
Is it possible at list to change the background color on focus for the entire drop down List?
Not true, select options can be styled (there are just some limitations in some browsers) *cough* IE *cough* http://webbugtrack.blogspot.com/2007/11/bug-291-no-styling-options-in-ie.html
scunliffe
+1  A: 

This should work in most browsers:

<html>
<head>
<style type="text/css">
option.red {background-color:red}
option.blue {background-color:blue}
option.white {background-color:white}
</style>
</head>

<select>
<option value="item 1" class="red">Item 1</option>
<option value="item 2" class="blue">Item 2</option>
<option value="item 3" class="white">Item 3</option>
</select>
</html>

This proves that individual items can be coloured differently, but I'm not sure about changing on the OnFocus event.

_J_
CSS wise - yes you can change the background color. However for events, IE will only fire them on the select element NOT on the option elements. Likewise IE has very limited styling options on the options themselves. Thus you can't do things like set a background image or adjust the individual text-indent/padding.
scunliffe
A: 

You could use J's CSS example or create templated drop down with jQuery. There's a good number of plugins that are simple to implement. Here's an example. This one even allows images.

gmcalab