How can I compare two hexadecimal values?

I need to compare two hexadecimal values ​​that come from the attribute field of an xml tag, I'm trying to do this:

var fill = $(this).attr( "fill" );
// console.log( fill.toString(16) );
if ( fill === "#FF00FF" )

But no ideas work?

+3
source share
3 answers

attrreturns a string, there is no need to name toStringit (and the argument will be ignored because it String toStringdoes not accept the argument).

Your code takes a few things:

  • That the attribute is returned in the form of #hex (if it is a color value, it is an unreliable true cross browser).

  • This will be in all the upper cases.

Not knowing what you see when registering the value, I simply address the second part:

var fill = $(this).attr( "fill" );
if ( fill.toUpperCase() === "#FF00FF" )
+2
source

I think you should use 2 identical signs there, try this ...

var fill = $(this).attr( "fill" );
if ( fill == "#FF00FF" )

, , , $(this)

+2

If fill is a color, then it can be returned in RGB format. And when you write it, you write toString(). Either compare it with an RGB value, or compare it with a string asfill.toString(16)

0
source

All Articles