Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I compare two hex values?

Tags:

javascript

I need to compare two hex values that are coming from a xml tag attribute field, I'm trying this:

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

But is not working any ideas?

like image 210
Ricardo Sanchez Avatar asked Oct 08 '22 20:10

Ricardo Sanchez


1 Answers

attr returns a string, there's no need to call toString on it (and the argument will be ignored, because String's toString doesn't take an argument).

Your code is assuming a couple of things:

  1. That the attribute comes back in #hex form (if it's a color value, this is not reliably true cross-browser).

  2. That it will be in all upper case.

Not knowing what you see when you log the value, I'll just address the second part:

var fill = $(this).attr( "fill" );
if ( fill.toUpperCase() === "#FF00FF" )
like image 83
T.J. Crowder Avatar answered Oct 10 '22 10:10

T.J. Crowder