views:

102

answers:

3

I have this HTML structure:

<div class="start">
  <div class="someclass">
    <div class="catchme">
      <div="nested">
        <div class="catchme"> <!-- STOP! no, no catchme's within other catchme's -->
        </div>
      </div>
    </div>
    <div class="someclass">
      <div class="catchme">
      </div>
    </div>
  </div>
  <div class="otherclass">
    <div class="catchme">
    </div>
  </div>
</div>

I am looking for a JQuery structure that returns all catchme's within my 'start' container, except all catchme's that are contained in a found catchme. In fact I only want all 'first-level' catchme's regardless how deep they are in the DOM tree.

This is something near, but not really fine:

var start = $('.start');
// do smething
$('.catchme:first, .catchme:first:parent + .catchme', start)

I sort of want to break further traversing down the tree behind all found elements. Any ideas?

+3  A: 

Will this do?

$('.catchme:not(.catchme .catchme)');
J-P
Great. That's thinking out of the box. I looked for finding the elements and not for excluding the wrong ones. Thank you alot.
Sebastian P.R. Gingter
A: 

Would something like this work?

$('.catchme :not(.catchme)', start)
monkeyninja
This wouldn't work. It will return *all* elements descending from `.catchme` that are not `.catchme`. OP has an element called `nested` that would be returned, and it wouldn't actually return the top level `.catchme` elements.
patrick dw
A: 

You can use .filter() and .parents():

$('.catchme').filter(function(){
    return $(this).parents('.catchme').length == 0;
};

This will select only those catchme elements that don't have a catchme element as ancestor.

Felix Kling