1

i'm trying getting elements background colors

$(document).ready(function(){
    $.each('.log-widget',function(){
    console.log($(this).css('backgroundColor'));
    //$(this).css({'box-shadow':'1px 1px 20px'+});
   });
  });

it doesn't works it send me back :TypeError: invalid 'in' operand a

i would like to grab background-color of each element and return that into hex color.

itsme
  • 48,972
  • 96
  • 224
  • 345

2 Answers2

3

You're iterating a string which is not what you're after. You want .each:

$(".log-widget").each(function() { ... });

Alternatively, you can use $.each, but with a jQuery set:

$.each($(".log-widget"), function() { ... });

In any case, you will have to create a set from the selector string.

pimvdb
  • 151,816
  • 78
  • 307
  • 352
2

As @pimvdb pointed out, your .each syntax was wrong. To return hexadecimal colors, though, there isn't a native way. You'll have to do the conversion yourself. Take a look at this answer to a similar question: little link.

Community
  • 1
  • 1
Chris
  • 26,544
  • 5
  • 58
  • 71
  • 1
    +1, good point. I noticed that on Chrome, the solution you linked to only accounts for elements in the DOM. Disconnected elements don't seem to have a color like `red` converted to hex/rgb. – pimvdb Sep 30 '12 at 11:27