jQuery Each Function Inside a jQuery Each Function

19,234

Solution 1

The jQuery $.each() function is just a loop. It iterates through the item that the selector returns. Since this is the case, if you select every UL on the page, then while viewing that UL item, you select all li items for that UL, then so on and all spans in each li. You can go on forever if the items are there. It doesn't matter, it is just a loop.

 $.each("ul", function(index, element)
 {
      var $this = $(this);
      var $items = $this.find("li");
      //now next loop
      $.each($items, function(n, e)
      {
          //this is each li in this ul
      });
 });

Solution 2

Like this:

jQuery.each([[1,2], [3,4], [5,6]], function(index, value) {
  jQuery.each(value, function(subindex, subvalue) {
    console.log(subvalue);
  });
});

Outputs:

1
2
3
4
5
6
Share:
19,234
LookingLA
Author by

LookingLA

Web Designer at Looking

Updated on June 20, 2022

Comments

  • LookingLA
    LookingLA about 2 years

    I can't figure out if this is possible, basically want to run an each function on every article and run another each function inside that each function on every li in that article, is this possible? I can post more details later.

    • Sergio
      Sergio over 10 years
      Yes. Post more details.
    • bipen
      bipen over 10 years
      hell yeah!! its possible.. why do you think that is not possible..
    • Sparky
      Sparky over 10 years
      When posting here, don't expect people to wait for "more details later". Put some effort into making the post as good as possible on your first attempt, and please don't ask for something that you can easily try out for yourself.
    • LookingLA
      LookingLA over 10 years
      Hey Sparky, I did try things and searched around and that's why I posted, I couldn't find anything quickly. Sometimes documentation is difficult to understand without contexts, we aren't all trained programmers here, that's why we're here in the first place, to learn from example and to ask when we don't find. I feel like this question is unique enough to warrant the post, if I am wrong, then show me where this has been answered, because I couldn't find it, and don't say it's easy to figure out yourself, everyone is at different levels of understanding.
  • keeehlan
    keeehlan almost 10 years
    What's the point of passing index, element if you're not going to use either of them?
  • Casey ScriptFu Pharr
    Casey ScriptFu Pharr over 9 years
    Just displaying that you can use them incase the user was not aware of the function.