Reputation: 99
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
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
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
Reputation: 2466
$linkCollection.find('ul li').each(function(){
// do your stuff here
});
Upvotes: 3
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