0

Given $('#test') and the following DOM, how can I select the corresponding ted-collapse?

<div id='test' class='ted-panel'>
  <div class='ted-collapse'></div>

  <div class='ted-panel'>
    <div class='ted-collapse'></div>
    <div class='ted-collapse'></div>
    <div class='ted-collapse'></div>
  </div>
</div>

Notes:

  • The corresponding .ted-collapse is the element that's a child of the current .ted-panel and not a child of any nested .ted-panel
  • .ted-collapse is not guaranteed to be an immediate child of .ted-panel

Essentially, I want to (1) filter out any children .ted-panels, then (2) .find('.ted-collapse).


Here's a one-line solution based on one of the comments:

$('#test').not($('#test').find('.ted-collapse'))
Sam Selikoff
  • 12,366
  • 13
  • 58
  • 104

3 Answers3

1

You can go this way too:

$('.ted-panel .ted-collapse:not(#test.ted-panel .ted-panel .ted-collapse)')

working demo

Bhojendra Rauniyar
  • 83,432
  • 35
  • 168
  • 231
0

Use the > selector

The element>element selector is used to select elements with a specific parent. Note: Elements that are not directly a child of the specified parent, are not selected.

$('#test > .ted-collapse')
Scott Murphy
  • 448
  • 2
  • 12
0

Get #test, find elements classed as .ted-collapse, but exclude elements that are direct children of elements that are direct children of #test:

$('#test').find('.ted-collapse:not(#test > .ted-panel .ted-collapse)');

Or, if #test isn't the right initial selector, maybe

$('div.ted-panel:first-child').find('.ted-collapse:not(div.ted-panel:first-child > .ted-panel .ted-collapse)');

See http://jsfiddle.net/jhfrench/gzjvsyb3/ for working examples.

Jeromy French
  • 11,812
  • 19
  • 76
  • 129