Correct HTML for select coloring

Need cross-browser color in your Web "pick-lists"? Here's what it takes.

By Cameron Laird  Add a new comment

We recently came across a fragment of HTML we needed that we could find documented nowhere -- so "Smart Development" explains it here.

Our Web applications manage scores of basic HTML forms. Many of them employ "pick-lists", or drop-down selection boxes of various sorts. For many of these, color provides value; for example, a management panel for virtual instances shows different background colors based on the geographic location of the base host: light blue, Vancouver, light yellow, Omaha, and so on. We've found that color makes certain data far easier to grasp at a glance.

As simple as that sounds, quite a few references document it incorrectly, and none (!) completely describe what it takes to have a selection show as its background color the background color of the current selection.

The situation and its solution have several parts. The option-s themselves take bgcolor, so it's easy enough to color the background of individual selections. In nearly all browsers, though, the background color of the "closed" selection is different, and must be assigned separately.

The drop-downs above illustrate much of this: the middle one, for instance, has "green" selected, and shows green (in most browsers) as its background color whether the drop-down is open or closed. This is conveniently done with the background-color style.

To have the color behave as we want after a user makes a selection requires an additional line of JavaScript; it can't be done declaratively, even with CSS3, to the best of our knowledge. JavaScript's access to the backgroundColor property, though, gives the following drop-down ...

... the ability to update the selection color in response to end-user clicks.

Central to this implementation is the assignment,

onChange="this.style.backgroundColor=this.options[this.selectedIndex].style.backgroundColor"

You can examine the rest of the pertinent source simply by viewing the source of this page. Note that different browsers handle "focus" with subtle differences. Note, also, that the drop-downs above are correct only with respect to specific requirements. We use different color behaviors for other drop-downs, in other contexts.

Has anyone else documented this more completely? This dialogue comes close, but provides slightly non-conformant capitalization for the style property. It's frustrating to have to take such pains in definition of what looks like a simple widget, especially when JavaScript is capable of so much more (two examples: musical notation; and a reactive, event-oriented, programming language). These details are essential, though, to provide an error-free experience for end users.

Coming up

Smart Development's next tip will touch on use of SVG as a "scratchpad" for visual design.

ITworld LIVE

DevelopmentWhite Papers & Webcasts

Webcast On Demand

How to Distribute Apps to Your Mobile Workforce

When considering enterprise app deployment, you may find some unexpected challenges and a number of options that range from simple distribution to running your own enterprise market. How can you determine the best approach for your organization? MOTODEV for Enterprise can help you understand and evaluate current enterprise deployment technologies and learn best practices that support your choice.

Sponsor: Motorola Mobility

Webcast On Demand

Authentication, Certificates and VPNs

MOTODEV for Enterprise can help get you up to speed quickly on key topics such as how to enable secure access to a company intranet from outside the firewall. This webinar provides a clear explanation of terms and technologies and what they can do for your enterprise app development.

Sponsor: Motorola Mobility

Webcast On Demand

Improving Enterprise App Quality with MOTODEV App Validator

MOTODEV for Enterprise supports quality app development for businesses, government, and institutions with technical resources and tools such as the MOTODEV App Validator, a free static analysis testing tool.

Sponsor: Motorola Mobility

White Paper

HR Analytics: Driving Return on Human Capital Investments

In today's economy, it's critical for organizations to make employee retention and development a major business focus, to ensure that valuable employees are not lost as the economy improves. With advanced BI solutions, organizations can be supported by workforce analytics to drive return on human capital investment and to see the value the workforce delivers to organizational performance. This white paper demonstrates how the increased power of having metrics and analytic insight can align core HR business processes with organizational goals and strategies and help ensure organizations make the right business decisions today for tomorrow.

White Paper

Positioning the CIO as a Powerful Business Partner with IT Portfolio Governance

In this whitepaper, learn how you can become a visionary portfolio manager and transform IT into a streamlined revenue and profit center.

See more White Papers | Webcasts

Ask a question

Ask a Question