navigate the child elements

1

Would you like to know how to loop through all child elements?

    
asked by anonymous 16.01.2015 / 18:30

2 answers

8

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 .

    
16.01.2015 / 18:44
2

You can use the Each () of jquery

link

<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) {

});
    
16.01.2015 / 18:34