File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes HTML, CSS and JavaScript and the fly likes dynamic color Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Engineering » HTML, CSS and JavaScript
Bookmark "dynamic color" Watch "dynamic color" New topic
Author

dynamic color

Graham Clark
Greenhorn

Joined: Jun 13, 2002
Posts: 17
Hi,

I'm looking for a way to dynamically change the color of an HTML element using CSS and Javascript, by changing an element's current RGB values. At the moment, I'm setting the color of an element which has an id of "tab1" with the line:

document.getElementById("tab1").style.backgroundColor="#D6B20A";

but then, if I do

alert(document.getElementById("tab1").style.backgroundColor)

instead of being presented with "#D6B20A", I get "rgb(214, 178, 10)".

It would be fine if I could set the red, green and blue attributes separately, or if I just had the hexadecimal code to change, but at the moment I seem to have a mixture of both, which is not making things easy.

Is there some way I can work with either hexadecimal codes, or individual RGB values, but not both?

Thanks for any help provided.

Graham.
[ August 09, 2004: Message edited by: Graham Clark ]
Eric Pascarello
author
Rancher

Joined: Nov 08, 2001
Posts: 15376
    
    6
The property shows whatever is stored into the property. If you store the rbg then you get that, if you store the hex, you get that.

If you want tpo go between the two, then you need to use a function that converts them. If you search google, there are plenty of JavaScripts that convert from Hex.

Eric
Graham Clark
Greenhorn

Joined: Jun 13, 2002
Posts: 17
But, I set the property as hex, but got back rgb!

My problem isn't converting between hex and rgb, though.
What I want is to set an element with an initial color, and then get that color when the user triggers some event, then change the color by, say, subtracting 10 from the red value. How can I do this?
sorry if i didn't explain this well previously.
Eric Pascarello
author
Rancher

Joined: Nov 08, 2001
Posts: 15376
    
    6
But, I set the property as hex, but got back rgb!


What browser is this happening in?
Graham Clark
Greenhorn

Joined: Jun 13, 2002
Posts: 17
ok, I just realised that this only seems to happen in Firefox 0.9.3, not Internet Explorer (6). So I guess it's a browser bug.

So if I want to set the color as an RGB value, can I use some function like the one Firefox seems to return to me? It gives "rgb(x, y, z)", so is there a function rgb() somewhere in Javascript?
Eric Pascarello
author
Rancher

Joined: Nov 08, 2001
Posts: 15376
    
    6
you can set colors with the rgb, it is part of the CSS so you can adjust the colors easier.

Below is a simple script for changing the background color.



Eric
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: dynamic color
 
Similar Threads
javascript event ?
Changing the TextInput Colour while displying Results from database
Finding element with specified color
SVG Components
Convert Color (RGB) to Hex