jammypeach jammypeach - 1 year ago 99
jQuery Question

jquery find() - how to exclude certain descendants, and their children?

I have markup similar to this:

<div class='wrapper plugin'>
//some content
<div class='child-wrapper plugin'>
//some more content
<li><div class='cliky'>clicky!</div></li>
<div class='cliky'>clicky!</div>

I need to be able to select the first
div only, not any
divs inside a
.plugin > .plugin

The wrapper has
class too - might seem counter-intuitive so another way to view it is this: I want to get the
children of a given
div, but not
.plugin .plugin .clicky

I've tried selectors like:

$('.wrapper').find('.clicky').not('.plugin > .clicky');

But they still selected all child
elements - including those of the nested

How would I be able to select only the
.plugin .clicky
elements of a particular
div, but not any
.plugin .plugin .clicky

Answer Source

Try this:

$('.wrapper .clicky').filter(function(){
   return $(this).parents('.plugin').length === 1
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download