Would you like to know how to loop through all child elements?
Would you like to know how to loop through all child elements?
There are several ways to do this. You can use children
or find
, optionally using some more specific selector. To loop through the elements, you can use each
.
// percorre todos os filhos
$(elem).children().each(function() {
console.log($(this));
});
// percorre todos os filhos
$(elem).find("> *").each(function() {
console.log($(this));
});
// percorre todos os li's filhos
$(elem).children("li").each(function() {
console.log($(this));
});
// percorre todos os li's filhos
$(elem).find("> li").each(function() {
console.log($(this));
});
I think using children
is the best option as it gets clearer what you want to do, and it will not go through all the elements below the parent element in the search for those closing with the selector like find
does .
You can use the Each () of jquery
<ul id="column1">
<li rel="1">Info</li>
<li rel="2">Info</li>
<li rel="3">Info</li>
</ul>
<ul id="column2">
<li rel="4">Info</li>
<li rel="5">Info</li>
<li rel="6">Info</li>
</ul>
<ul id="column3">
<li rel="7">Info</li>
<li rel="8">Info</li>
<li rel="9">Info</li>
</ul>
$('ul li').each(function(i) {
});