[Greasemonkey] change background color of options
lists at atrus.org
Wed Jul 6 10:36:35 EDT 2005
Tobias Orterer wrote:
>is it possible to change the background color of an single <option>
>element in a select box?
>like (written html):
><option style="background-color:#FF0000;"> </option>
><option style="background-color:#00FF00;"> </option>
>thats for your help!
The above HTML works as you'd expect, at least in Firefox 1.0.4. The
only catch is that once you select the value, the color vanishes. To get
it to work, you have to set the background-color of the select element:
Obviously you'd want to change it based on the currently selected item.
You can use elm.style.setProperty( "background-color", "#FF0000", "" ),
assuming elm has an HTMLElment object (which is what you get from
getElementByID and such).
The index of the currently selected option is stored in selectedIndex.
options can be quickly accessed via the options proerty.
elm.options[ elm.selectedIndex ]
will yield the currently selected option. The following code sets the
background color of the select element to be the same a s the currently
curColor = select.options[ elm.selectedIndex ].style.backgroundColor;
select.style.setProperty( "background-color", curColor, "" )
More information about the Greasemonkey