Possible Duplicate:
jQuery filtering selector to remove nested elements matching pattern.
I have a hierarchy of groups. Something like:
<div class="group">
<span class="child"></span>
<div class="group">
<span class="child"></span>
<span class="child"></span>
<span class="child"></span>
</div>
<span class="child"></span>
<span class="child"></span>
<div>This child is farther down <span class="child"></span></div>
</div>
How can I select the children in each group, without selecting any subgroup's children?
$(".group").each(function() {
// Wrong, click fires twice (the 1st level group selects 2nd level children)
var children = $(this).children(".child");
// Wrong, click fires twice
children = $(this).children(":not(.group) .child");
// How can I properly do this??
children.click(function() {
alert("children.click");
});
});
I've also tried find()
instead of children()
but I can't seem to get this to work correctly. Also, I can't use direct children (or >
) because the descendants could be inside other non-.gorup HTML tags (i.e. several DOM levels down).