views:

12

answers:

1

I have a collection of div's which I need to remember and loop through, easy.

var myCollection = $('div.myClass');
$.each(myCollection, function(myDiv){...});

Now I want to select some span tags in each of those div's but only those that are direct children of the div. This kinda works...

$.each(myCollection, function(myDiv){
    $('span.error', $(myDiv)).each(function(){...});
});

I don't want it to work in the following scenario

<div class="myClass">
    <div class="myClass">
        <span class="error"></span>
    </div>
</div>

[If I didn't need to save the collection I could have used a child selector div.myClass > span.error]

+2  A: 

Try this:

$('div.myClass').each(function() {
  $(this).children('span.error').each(function() {
    var span_error = $(this);
  });
});
jcubic