background-color hex to js variable (jquery)

Posted by Ezdaroth on Stack Overflow See other posts from Stack Overflow or by Ezdaroth
Published on 2009-03-12T14:44:23Z Indexed on 2010/05/22 18:40 UTC
Read the original article Hit count: 278

Filed under:
|
|
|
|

I'm kinda new to javascript and jquery and now I'm facing a problem:

I need to post some data to php and one bit of the data needs to be the background color hex of div X.

Jquery has css("background-color") function and with it I can get rgb value of the background into a javascript variable.

The css function seems to return a string like this rgb(0, 70, 255).

I couldn't find any way to get hex of the background-color (even tho it's set as hex in css).

So it seems like I need to convert it. I found a function for converting rgb to hex, but it needs to be called with three different variables, r, g and b. So I would need to parse the string rgb(x,xx,xxx) into var r=x; var g=xx; var b=xxx; somehow.

I tried to google parsing strings with javascript, but didn't really understand the regular expressions thing.

Could someone tell me if there's a way to get background-color of div as hex, or explain how to convert the string into 3 different variables.

© Stack Overflow or respective owner

Related posts about JavaScript

Related posts about jQuery