Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Explanation of two arguments for jQuery .each() method

In the following jQuery, the .each() method takes two arguments: 'ul li a' and menu. What do these two arguments mean?

var menu = $('.menu');
$('ul li a', menu).each(function() {
    $(this).append('<span />');
});

HTML:

<div class="menu">
    <ul>
        <li><a href="#">Edit Profile</a></li>
        <li><a href="#">Account Settings</a></li>
        <li><a href="#">Appear Offline</a></li>
        <li><a href="#">Logout</a></li>
    </ul>
</div>
like image 458
Istiaque Ahmed Avatar asked Dec 31 '11 05:12

Istiaque Ahmed


People also ask

What is use of each () function in jQuery?

jQuery's each() function is used to loop through each element of the target jQuery object — an object that contains one or more DOM elements, and exposes all jQuery functions. It's very useful for multi-element DOM manipulation, as well as iterating over arbitrary arrays and object properties.

What does each () do?

each(), which is used to iterate, exclusively, over a jQuery object. The $. each() function can be used to iterate over any collection, whether it is an object or an array. In the case of an array, the callback is passed an array index and a corresponding array value each time.

Can we use foreach in jQuery?

each() function, jQuery's foreach equivalent. jQuery's foreach equivalent can be very useful for many situations. These examples will get you started and teach you how you can loop through arrays, objects and all kinds of HTML elements.


2 Answers

The second parameter to the jQuery function is the context. This tells jQuery to only search for elements that are in that context

So for your example:

$('ul li a', menu).each(function() {
    $(this).append('<span />');
});

This will only search for anchors within li's within a ul, that are also located inside of menu.

This can be a very useful optimization since it can significantly cut down the amount of space jQuery has to search.

EDIT

To possibly make this a bit clearer (as jfriend00 points out), your particular example, $('ul li a', menu), is equivalent to $('.menu ul li a')

The context parameter is explained here in the docs

like image 60
Adam Rackis Avatar answered Oct 13 '22 18:10

Adam Rackis


Hello Brother Assalamu Alaikkum,
The explanation is as follows,
The first parameter is 'ul li a' It means that it searches for the anchor tag within the list item tag which in turn present inside the unordered list tag. This first argument is the sub context.

The second parameter menu which is the main context. That is jquery searches for the elements with the class name as "menu".

Finally jquery searches the sub context within the main context. So the jquery searches for the main context "menu" first and then it searches the sub context within the main context. The keyword "each" denotes all the sub elements to be searched within the main element.

hope this helps you.

like image 35
AmGates Avatar answered Oct 13 '22 18:10

AmGates