Assuming the following:
<div class="a"> <!-- select -->
<div class="b">
<div class="a"> <!-- skip -->
</div>
</div>
</div>
<div class="b">
<div class="a"> <!-- select -->
<div class="b">
<div class="a"> <!-- skip -->
</div>
</div>
</div>
</div>
<div class="a"> <!-- select -->
</div>
How do I only select the outermost $('.a') elements?
Edit: A helpful JSFiddle, in this example, only 'a' should be selected, not 'a!'.
This is where filter methods come in handy:
$('.a').not('.a .a');
This excludes any .a that is nested within another .a so you only get the outermost ones, regardless of whether the outermost ones are themselves nested within other elements.
Try using the jQuery :not selector
$('.a:not(.a .a)')
http://jsfiddle.net/7E7Mk/1/
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With