Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Change color of select box based on option chosen

Status
Not open for further replies.

rmagan

Programmer
Jun 3, 2003
35
US
I would like to change the color of a select box based on what option they choose. In the example below, I would change the color to red, yellow or green. Is this possible?

<html>
<head>
</head>

<body>
<SELECT name=&quot;p_param1&quot;>
<OPTION value=RED>STOP
<OPTION value=YELLOW>CAUTION
<OPTION value=GREEN>GO

</SELECT>


</body>
</html>
 
<html>
<head>
<script>
function getColor(){
theSelect = document.getElementById(&quot;p_param1&quot;)
theColor = theSelect.options[theSelect.selectedIndex].value
theSelect.style.backgroundColor = theColor
}
</script>
</head>

<body>
<SELECT id=&quot;p_param1&quot; onChange=&quot;getColor()&quot;>
<OPTION value=RED>STOP
<OPTION value=YELLOW>CAUTION
<OPTION value=GREEN>GO

</SELECT>


</body>
</html>

Programming today is a race between software engineers striving to build better and bigger idiot-proof programs, and the Universe trying to produce bigger and better idiots. So far, the Universe is winning. - Rick Cook (No, I'm not Rick)

fart.gif
 
Just this function:
Code:
function changeColour( el ) {

	el.style.backgroundColor = el.options[el.options.selectedIndex].value;
}

... called like this from your select ...
Code:
<SELECT name=&quot;p_param1&quot; onchange=&quot;changeColour( this );&quot;>
 
(LOL - Wolfy, even when I hit Refresh to check for new posts just before I post my own, somehow I end up doing this all the time.)
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top