1

How is that this works (div cirlcedecider2 has is styled "green")

function badge1(){
if (document.getElementById("circledecider2").style.color == "green"){

document.getElementById("badge1").style.backgroundColor= "#ABCF37";
document.getElementById("badge1").style.width = "200px";
document.getElementById("badge1").style.height = "200px";

}}

But this does not? (div cirlcedecider2 has is styled "#ABCF37")

function badge1(){
if (document.getElementById("circledecider2").style.color == "#ABCF37"){

document.getElementById("badge1").style.backgroundColor= "#ABCF37";
document.getElementById("badge1").style.width = "200px";
document.getElementById("badge1").style.height = "200px";

}}

The only difference is using a hexcode.

craig
  • 571
  • 2
  • 4
  • 13

3 Answers3

0

#ABCF37 gets parsed and changed into a different format. The result is most likely browser specific.

document.body.style.color = "#ABCF37"
document.body.style.color; // "rgb(171, 207, 55)"

And obviously

"rgb(171, 207, 55)" == "#ABCF37"; // false

You will need to write some function to interpret the colours and return something standard you can compare against (possibly in a rgba format), or a function to compare two colours (which may be easier).

function compareColour(col1, col2) {
    var e = document.createElement('span')
    document.body.appendChild(e);
    // standardise
    e.style.color = col1;
    col1 = window.getComputedStyle(e).color;
    e.style.color = col2;
    col2 = window.getComputedStyle(e).color;
    // cleanup
    document.body.removeChild(e);
    return col1 === col2;
}

compareColour("#ABCF37", "rgb(171, 207, 55)"); // true
Paul S.
  • 64,864
  • 9
  • 122
  • 138
0

Reference color

function badge1(){
if (document.getElementById("circledecider2").style.color == "rgb(171, 207, 55)")
{

document.getElementById("badge1").style.backgroundColor= "#ABCF37";
document.getElementById("badge1").style.width = "200px";
document.getElementById("badge1").style.height = "200px";

}}
Sasidharan
  • 3,676
  • 3
  • 19
  • 37
0

The color you retrieve is browser specific.

In IE you can see the browser returning hex value, for that too your code should compare the alphabets in hex values in small case i.e. #abcf37 in your case.

Other browsers like chrome return it in rgb format.

Alternate solution is to convert the value in hex format if returned in rgb.

Refer answer here for the alternate solution.

Community
  • 1
  • 1
niksvp
  • 5,545
  • 2
  • 24
  • 41