views:

54

answers:

2

how can i get for example all links inside a all ready selected jquery element (this)

$("#container li").each(function(){
   $("this a").each(function(){
      // links inside this li element
   });
});

This does not work is there a other way?

+6  A: 

You could use the .find() function:

$('#container li').each(function() {
    $(this).find('a').each(function() {
        // links inside this li element
    });
});

or to avoid nested loops you could directly select the links and then fetch the parent li if needed:

$('#container li a').each(function() {
    var parentLi = $(this).parent('li');
});
Darin Dimitrov
off course thx!
Remi
+3  A: 

Alternatively to Darin's proposal, jQuery allows you to define a context node for a selector.

So, you could do this:

var
  $listItems = $('#container li'),
  // use $listItems as context
  $anchors = $('a', $listItems);
Thomas
Or, a [ **little more directly** ](http://jsfiddle.net/b9S4p/): `$('a', '#container li')`
Peter Ajtai
Sure, though as I understood it, this was about reusing elements. Your version seems to defeat the purpose entirely.
Thomas