jquery selecting children not nested in a child

html, javascript, jquery, jquery-selectors

Solution

Based on Jquery: Get all elements of a class that are not decendents of an element with the same class name? I've developed the following solution. Thanks to all.

$.fn.findButNotNested = function(selector, notInSelector) {
    var origElement = $(this);
    return origElement.find(selector).filter(function() {
        return origElement[0] == $(this).closest(notInSelector)[0];
    });
};

Problem

I have some problems with a jQuery selector. Let's say I have the following html where `(...)` stands for an undefined number of html tags. ``` (...) <div class="container"> (...) <div class="subContainer"> (...) <div class="container"> (...) <div class="subContainer"/> (...) </div> (...) </div> (...) </div> (...) ``` Let say that I have a javascript variable called `container` that points to the first div (with class container). I want a jquery that selects the first subcontainer but not the nested one. If I use`$(".subContainer", container);` I'll get both of them. I've tried using ``` $(".subContainer:not(.container .subContainer)", container); ``` but this returns an empty set. Any solution? Thanks.

Original source

Related problems