views:

35

answers:

3

I know about the attributeContains selector, but how does it apply to style attributes?

I want to find all <a> tags that have their opacity set to 0.

I tried this :

$("a[style*='opacity: 0']")

But it returns nothing.

+1  A: 

If you want to know whether or not an element is visible, use:

$('a:not(:visible)');
jAndy
Geeze, we are quick to the draw eh ;) Wouldn't want to verse you in a duel.
balupton
+1  A: 

$('a:not(:visible)')

Your code won't work as it only works when the opacity is applied on the element's style attribute - what about CSS styles??? they won't apply. jQuery provides the :visible and :not selectors, so you can combine them. http://api.jquery.com/category/selectors/

balupton
+3  A: 

The :visible selector will not work because it doesn't give consideration to opacity.

To target just those with a 0 opacity, you could use a .filter() to check the .css() value of the opacity:

$("a").filter( function() {
    return $(this).css('opacity') === '0';
});

You could create your own selector if you'd like:

$.extend($.expr[':'], {
    opacity: function(elem, i, attr){
      return( $(elem).css("opacity") === attr[3] + '' );
    }
});

var $invisible = $("a:opacity(0)");

or

$.extend($.expr[':'], {
    transparent: function(elem, i, attr){
      return( $(elem).css("opacity") === "0" );
    }
});

var $invisible = $("a:transparent");
patrick dw
+1 Exactly what I was thinking. "Elements with visibility: hidden or opacity: 0 are considered to be visible, since they still consume space in the layout." http://api.jquery.com/visible-selector/
Josh Stodola