views:

46

answers:

3

Trying to catch td with style:

<td style="color: #333;">

Hope this should work, but it doesn't:

td:[style='color: #333;']

Any idea?

Thanks.

A: 
$('td[style="color: rgb(51, 51, 51);"], td[style="color: #333;"], td[style="COLOR: #333"]')

This works in Explorer 8, Firefox, Safari and Chrome demo

Samuel
+1............. and I added some demo.. ;)
Reigel
It doesn't work. Alerts 0
rahul
mine alerted 1.. well, I'm on chrome... FF alert 0...
Reigel
mine alerted 1. Google Chrome on Ubuntu
Tim Büthe
Just tried your demo - returns 0 - so doesn't work at all! (FF 3.6.3)
Jamiec
In IE 7 also it alerts 0.
rahul
updated answer for support of explorer 8 and firefox
Samuel
+1  A: 

Seems like a missing feature.

$.fn.hasStyle = function(style){
   return this.filter(function(){
     return ($(this).attr('style').indexOf(style) > -1)
   }).length > 0;
};

Synopsis:

$('td').hasStyle('color: #333');

In your case it could be a custom selector even:

$(document).ready(function(){
  $.extend($.expr[':'], {
       hasStyle: function(e, i, arg){
          var s = new String($(e).attr('style'));        
          return( s !== 'undefined' && s.indexOf(arg[3]) > -1 );
       }
  }); 
});​

Synopsis:

 $('td:hasStyle("color: #333")').fadeOut('slow');

working example:

http://jsbin.com/atavu3/edit

jAndy
The custom selector is a great solution IMO.
Jamiec
correct me with what I have based on your custom selector... it won't get one... http://jsfiddle.net/ysr6t/2/
Reigel
well, it looks like I needed to specify a string to use `.indexOf`. Fixed that.
jAndy
still won't work... cause some browser (like FF) shows color as `rgb(51,51,51)` as not like the other browser ( like Chrome ) as `#333`...
Reigel
well, I'm guessing the OP had make it work with this.. cheers!
Reigel
it's just a kickoff idea not a complete ready solution. Might be extended.
jAndy
The custom selector doesn't seem right here, it both depends on the style being in-line (and not a class either), and being *exactly* that syntax, e.g. `color:#333` wouldn't match.
Nick Craver
@Nick: telling you the same thing, if you have a better idea go ahead and edit the code (it's SO!). Make use of `e.style` or whatever if you feel like it.
jAndy
It's slower, but using `.css()` would be the *correct*, yet *expensive* approach...I have to get ready for the morning, I'll add an *alternative* way to do it, leaving the current soon as I'm back.
Nick Craver
A: 

I suspect the reason it doesn't work is because some browsers will "normalise" values. So you might find that in parsing it actually changes the #333 to a more standard 6 character colour string #333333 (or maybe even an rgb() style one). I have to admit I can't think of a way around this off the top of my head though but you should be able to cofnirm if this is the case with a few simple tests to read the current values.

Chris