Once use Chrome, console.log($("#some-element").css("background-color"))
, it returns rgba(255, 255, 255, 255)
, which means white
, how I can check whether an element's background color is white?
Asked
Active
Viewed 1,583 times
0

Atul Arvind
- 16,054
- 6
- 50
- 58

coderz
- 4,847
- 11
- 47
- 70
-
7FYI `rgba(0,0,0,0)` is black with `0` opacity. – Rory McCrossan May 08 '14 at 07:00
-
I'm not sure what you are looking for. Are you running some automated tests? – Justus Romijn May 08 '14 at 07:00
-
1White is (255, 255, 255) in RGB – Matthew Herbst May 08 '14 at 07:00
-
I just want to check whether an element's background color is white. – coderz May 08 '14 at 07:01
-
rgb(255, 255, 255) this is white – Sudharsan S May 08 '14 at 07:02
-
1I use Chrome, if one element has no clearly defined ```background-color``` attribute, it returns ```rgba(0,0,0,0)``` instead of ```undefined```, then is it dangerous to say an element has no definition of ```background-color``` if ```$("#some-element").css("background-color") == "rgba(0,0,0,0)"``` returns ```true```? – coderz May 08 '14 at 07:07
-
Worth checking out the answer at : http://stackoverflow.com/questions/5999209/jquery-how-to-get-the-background-color-code-of-an-element – Roy M J May 08 '14 at 07:11
-
http://jsfiddle.net/9CNJz/ – Ram May 08 '14 at 07:19
-
How to check whether an element defined ```background-color```? When use Chrome, it returns ```rgba(0,0,0,0)```, while when use Firefox, it returns ```transparent```. – coderz May 08 '14 at 07:42
3 Answers
2
You could use the code described in How to get the background color code of an element? where the color's form is converted to its code. You could fetch the codes of the colors you would like compare and than compare the codes.
-
How to check whether an element defined ```background-color```? When use Chrome, it returns ```rgba(0,0,0,0)```, while when use Firefox, it returns ```transparent```. – coderz May 08 '14 at 07:42
-
@coderz As discussed in http://stackoverflow.com/questions/5380431/check-if-background-color-is-undefined I would see what value value is returned for each of the major browsers and hardcode its conversion to an appropriate more value. – nbanic May 08 '14 at 07:49
0
In JS
if (document.getElementById('someElementId').style.backgroundColor == "rgb(255, 255, 255)");
In JQuery
if($('#someElementId').css('background-color')=="rgb(255, 255, 255)")

jhyap
- 3,779
- 6
- 27
- 47
-1
You can use:
if($("#some-element").css('background-color')=="rgb(255, 255, 255)")
console.log("White color detected");

Felix
- 37,892
- 8
- 43
- 55