user2215975
user2215975

Reputation: 99

Running an each() on a result of another each() in jquery

I am trying to build a mobile menu system for a website. Within the footer there are div's, with a class of .footer_menu. Within each of these div's is a ul with list of links relevant to this div.

What I would like to do is loop through each of these div's, which I can using the following:-

        $('.footer_menu').each(function () {
            var $linkCollection = $(this);
            // I would like to run something like
            ($linkCollection ul li).each(function(){
            code to go here to build menu. I can do this bit once I get the line above functioning.
            }
        })

Any necessarily looking for a solution, pointers in the right direction are fine.

Upvotes: 1

Views: 63

Answers (4)

zur4ik
zur4ik

Reputation: 6254

Try this:

$('.footer_menu').each(function () {
    var $linkCollection = $(this);
    // I would like to run something like
    $linkCollection.find('ul li').each(function(){
        //code to go here to build menu. I can do this bit once I get the line above functioning.
    }
})

Upvotes: 0

colestrode
colestrode

Reputation: 10658

The solution depends what you need to do. If you need to have access to the $linkCollection object, you could use nested each functions:

$('.footer_menu').each(function () {
    var $linkCollection = $(this);
    $linkCollection.find('ul li').each(function() {
        //do something here using $linkCollection
    });
});

Otherwise it would be much faster to do:

$('.footer_menu ul li').each(function() {
    //do something here
});

Upvotes: 1

Nagarjun
Nagarjun

Reputation: 2466

$linkCollection.find('ul li').each(function(){ 
  // do your stuff here
});

Upvotes: 3

km6zla
km6zla

Reputation: 4887

$('ul li', $(this)).each(function(){...

The format is $(selector, context). This will find all li elements in ul elements in the current .footer_menu iteration.

Upvotes: 1

Related Questions