views:

45

answers:

2

I'm trying to find all the parent elements that have the CSS style display:none. I can't seem to get it to work though. Here's what I've got:

var $parents = $(...).parents("[css=display:none]");
+2  A: 

If you want the ones that are actually display: none; (not just possibly contained in another display: none;), you can use .filter() and .css() to get those parents, like this:

var $parents = $(...).parents().filter(function() {
                  return $(this).css('display') == 'none';
               });

This gets the parents, then filters them to get only the ones that are display: none; on that specific parent.

Nick Craver
+2  A: 

@Nick's solution is a very simple and straightforward method of achieving your goal. It's also probably the best performing method. However, for the sake of completeness and convenience (if you're doing this a lot), it's also possible to create your own selector:

$.expr[':'].css = function(obj, index, meta, stack) {
    var args = meta[3].split(/\s*,\s*/);
    return $(obj).css(args[0]) == args[1];
}

// Then we can use our custom selector, like so:
$("#myElement").parents(":css(display,none)").show();

Example - http://jsfiddle.net/V8CQr/.

See more information on creating custom selectors at:

http://www.jameswiseman.com/blog/2010/04/19/creating-a-jquery-custom-selector/

Andy E