1

I didnt find an answer that I could understand. I am trying to test if a div that was clicked has a background color of 'red' ... instead of red (in an alert) I am getting rgb(125,0,0) .... tried testing that with single quotes and it didnt work... any help please?

 alert($('#' + id).css('background-color'));

 if ($('#' + id).css('background-color') == 'red') {
                 $('#' + id).hide('slow');
 }
Todd Vance
  • 4,627
  • 7
  • 46
  • 66

3 Answers3

3

Testing it in Chrome, I am getting spaces between those commas. If you are comparing strings, every character including spaces must be the same, so change to:

if ($('#' + id).css('background-color') == 'rgb(255, 0, 0)') {
David Tang
  • 92,262
  • 30
  • 167
  • 149
2

It would be a whole lot easier if you gave the divs the background color using a CSS class, then simply tested for the presence of the class. Color is less semantic than the class, anyway.

 <style>
    .alert { background-color: Red; }
 </style>

 <div id="warning" class="alert">
 </div>

 <script type="text/javascript">
    $(function() {
       $('div').click( function() {
           if ($(this).hasClass('alert')) {
                $(this).hide('slow');
           }
       });
    });
 </script>
tvanfosson
  • 524,688
  • 99
  • 697
  • 795
2

This may help to avoid any possible cross-browser difference.

Community
  • 1
  • 1
Quincy
  • 4,393
  • 3
  • 26
  • 40