views:

62

answers:

1

I need a way for jQuery to return all elements that have the css

font-family:AvantGardeITCbyBT-Bold, sans-serif;

applied to them.

I'm thinking the only way of doing this is looping through all elements and checking if this css is applied to it. Seems a slow way of doing it?

Is there a way of doing this via a jQuery Selector?

+1  A: 

well, you can extend the jQuery selectors with

$(document).ready(function(){
   $.extend($.expr[':'], {
     AvantGardel: function(elem){
        var $e = $(elem);
        return( typeof $e.css('font-family') !== 'undefined' && $e.css('font-family') === 'AvantGardeITCbyBT-Bold' );
     },
     SansSerif: function(elem){
       var $e = $(elem);
        return( typeof $e.css('font-family') !== 'undefined' && $e.css('font-family') === 'sans-serif' );
     }
 });    
});​

and then call

$(':AvantGardel').hide();

or

$(':SansSerif').hide();

for instance.

working example: http://jsbin.com/idova3/edit

jAndy
I was unable to get this to work.
Brady
http://block.pd.alphaready.com/ unable to get your code to function...
Brady
`$(':AvantGardel')` is gonna be dead slow.
J-P
@Scott Cariss: another update, try again
jAndy
@J-P: of course it's slow because you use it just like that. `:AvantGardel` equals `*:AvantGardel` which selects all elements. You should use that selector like `div:AvantGardel` for better performance obviously.
jAndy
Yeh, it's also slow because you're running `$.fn.css` twice, which isn't needed. I'd be tempted to implement a [flyweight](http://en.wikipedia.org/wiki/Flyweight_pattern) jQuery instance for this purpose too, so that you don't have to construct a new jQuery instance every time.
J-P
im sorry but I still cant get this to work on my webpage. I dont understand why it wont work when w=you have it working on jsbin...
Brady
J-P: whatever you say :p
jAndy