Search code examples
jqueryvisiblesiblings

How do I select an element that has no visible siblings using jQuery?


I know I could do this with some extra javascript, but I'm guessing there is a way to it using just a jQuery selector.

The element(s) I'm looking for will have siblings that have just (on the previous JS line) had their display property set to none. if the element(s) have even one visible sibling, I don't want a match.

Thanks a ton.


Solution

  • There might be a more concise way, but this should work:

    var siblings = $('#myItem').siblings(":visible").length;
    var item = siblings == 0 ? $('#myItem') : null;
    alert(item);
    

    You can see the fiddle here: http://jsfiddle.net/JmwcR/23/