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!

Changing DIV, background color

Status
Not open for further replies.

altendew

Programmer
Joined
Mar 29, 2005
Messages
154
Location
US
Hey right now I got my <div> displaying a background color.. I have a drop down list of color codes, and when going down the list I want the background color of the <div> tag to change so they can preview it.

This is the current code I have:
<select name="backgroundColor" onChange="document.backgroundColor.style = \"background-color: 000000\";">

......

<div id="backgroundColor" style="width: 20px; height: 20px; background-color:#003366; layer-background-color:#003366; visibility: visible"></div>

So how do I get these two to work together
 

Something like this should work:

Code:
onchange="document.getElementById('backgroundColor').style.backgroundColor = this.value;"

This assumes that the values of each of your options is valid CSS colour syntax, for example:

Code:
<option value="#000000">Black</option>
<option value="red">Red</option>

Hope this helps,
Dan


[tt]D'ya think I got where I am today because I dress like Peter Pan here?[/tt]
[banghead]

 
Well thanks I got it to work, but is this cross broswer functional?

 

Yes.

Dan


[tt]D'ya think I got where I am today because I dress like Peter Pan here?[/tt]
[banghead]

 
This isnt a big deal.

But when you press down button to go through the select list in Opera it doesnt change. Only when you click on the item. Works in all other browsers well though.
 

From what I can tell, the only browser (at least out of IE, NN, FF, and Opera) to fire the onchange event on a keypress - but BEFORE the control has lost focus - is IE.

This means that FF, NN, and Opera all behave the same.

Dan


[tt]D'ya think I got where I am today because I dress like Peter Pan here?[/tt]
[banghead]

 
If anyone is intrested I got it to work in all browsers by using.

<select name="backgroundTextColor" onKeyUp="document.getElementById('backgroundTextColorLayer').style.backgroundColor = this.value;" onKeyDown="document.getElementById('backgroundTextColorLayer').style.backgroundColor = this.value;" onchange="document.getElementById('backgroundTextColorLayer').style.backgroundColor = this.value;">
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top