I try to select only the first "layer" of children of this type, but not elements nested in another qualification element. For instance. in:
<div id='top'> <div id="s1" class="special"> <div id="s2" class="special"></div> </div> <div> <div id="s3" class="special"></div> </div> </div>
I would like to find # s1 and # s3, but not # s2, with something like $ ('# top'). find ('. special: not_nested'). Is this possible with jQuery? XPATH?
I was thinking about custom jQuery filters like expr [':']. not_nested, but I canβt understand how to take into account the top parent ($ ('# top') in this case, because there may be other special classes further in the parent chain #top.
[edit] I have to mention right now, I resort to a recursive call to $ .fn.children (), which I think is not very efficient.
[edit2] checked working code:
var node = $('#top'), result = (node.find('.special').filter(function(){ return !($(this).parent().closest(".special", node).length); }));
However, this does not work if "#top" has its own class. So maybe this:
var node = $('#top'), result= node.find('.special').filter(function(){ var parent = $(this).parent().closest(".special", node); return !parent.length || parent.is(node); });
source share