I have a situation in which i need to select all descendants of a certain element, but exclude those that are children of a container that equals the CSS class of the container i run my selector on.
Pretty complicated description.
<div class="container">
<div class="element"></div>
<div class="element"></div>
<div class="element"></div>
<div class="element">
<div class="container">
<div class="element"></div>
<div class="element"></div>
<div class="element"></div>
</div>
</div>
Running a jQuery .find('.element') on the outermost DIV will get me all the DIVs, even the ones inside the second container. That is what i try to avoid.
Is there a quick and simple jQuery selector solution for this case?
You can use jquery selector like this:
For your specific example this would work -
That will only get the top level element divs. If your element collection was in a different structure you could substitue
body
with the collection's container id.Demo - http://jsfiddle.net/QAP37/
I found the following slightly more succinct and general than the selected answer. It works with arbitrarily deeply nested trees:
where:
container
is the jQuery object for the root "container" divelementSelector
is the jQuery selector for the element,.element
in this casecontainerSelector
is the jQuery selector for the container,.container
in this caseSo this finds all the elements beneath the initial container, then for each element checks to see if its nearest container is the initial container or some other nested container. If its nearest container is a nested container, it throws it out, so you are just left with descendants which are not inside another container.
Very handy for things like repeating multi-element, nested form fields.
I think what you want to use is the not selector. Like this..
Alternately, you could use the children selector, to get the children from one level deep. Which would exlclude the nested divs.
To be a little more explicit, I think you'll want to use the not selector after you use the 'find'. Like this:
You can pass a function to not, so you could have that function look at the parents of each element match to see if it is nested inside of an element with the same class.
http://jsfiddle.net/QXfs2/6/
If you could add a class to the nested container, that would be ideal, then it's just:
Assuming you added the class "nested", to your inner container div.