What is the difference between $ .each (selector) and $ (selector) .each ()

What is the difference between this:

$.each($('#myTable input[name="deleteItem[]"]:checked').do_something()); 

and this:

 $('#myTable input[name="deleteItem[]"]:checked').each(function() { do_something }); 

The html for the table cell that is selected and valid is as follows:

 <td width="20px"><input type="checkbox" class="chkDeleteItem" name="deleteItem[]" value="' . $rowItem['itemID'] . '" /></td> 

I looked through the jQuery documentation, but I still don't understand the difference. (Is it me or is this documentation sometimes a little β€œhazy” in the clarity of the content?)

Information added:

Apparently, my attempt at typical examples confuses people! Along with the (previously) missing brackets in the first example .: (

The first example comes from a line in my code that removes <tbody> for any lines with a checkmark selected:

 $.each($('#classesTable input[name="deleteClasses[]"]:checked').parent().parent().parent().remove()); 

The second example comes from the situation when I look at #classesTable for all checked checkboxes and delete the corresponding item in the drop-down list.

 $('#classesTable input[name="deleteClasses[]"]:checked').each(function(){ $('#classesList option[value="' + $(this).attr('value') + '"]').remove(); }); 

I understand that they do two different things, but not so much that I can say: "I need to use $ .each () in this case and .each (function () {}) in another case.

Are they interchangeable at all? Only in some cases? Never?

+73
jquery each
Jul 07 2018-11-17T00:
source share
8 answers

Description:

.each is an iterator that is used to iterate through jQuery objects jQuery.each ( $.each ) is a common function for iterating over javascript objects and arrays.

<strong> Examples:

Javascript Array (or js object) using $ .each ():

 var myArray = [10,20,30]; jQuery.each( myArray, function(index, value) { console.log('element at index ' + index + ' is ' + value); }); //Output element at index 0 is 10 element at index 1 is 20 element at index 2 is 30 

jQuery objects using .each ()

 $('#dv').children().each(function(index, element) { console.log('element at index ' + index + 'is ' + (this.tagName)); console.log('current element as dom object:' + element); console.log('current element as jQuery object:' + $(this)); }); //Output element at index 0 is input element at index 1 is p element at index 2 is span 

If you are looking for additional examples + details, $. each vs .each ()

Resources

+91
Jul 07 '11 at 13:20
source share

from http://api.jquery.com/jQuery.each :

The $ .each () function is not the same as the .each () function, which is used to iterate over the jQuery object exclusively. The $ .each () function can be used to iterate over any collection, regardless of whether it is a map (JavaScript object) or an array.

+5
Jul 07 '11 at 13:21
source share

You really want to use $.each with an array that is not an element or anything else. those.:

 var x = ["test", "test2"]; 

You would use $.each(x... to traverse it instead of x.each :)

.each for elements only :)

+3
Jul 07 2018-11-11T00:
source share

The first will run the callback function on the items in the collection you are in, but your code is currently not syntactically correct.

It should be:

 $.each($('#myTable input[name="deleteItem[]"]:checked'), do_something); 

See: http://api.jquery.com/jQuery.each/

The second will run a function for each item in the collection in which you use it.

See: http://api.jquery.com/each/

+1
Jul 07 2018-11-11T00:
source share

There is no functional difference. Each jQuery object has a .each() method inherited from jQuery.fn . By invoking this object method , jQuery already knows why Array (-like object) iterates over. In other words, it moves through indexed propertys from the current jQuery object.

$.each() On the other hand, this is just a β€œhelper tool” that iterates over any types of Array or Object , but of course you have to say which method you want to iterate over. It will also take care of you whether you pass in an array or object, it does the right thing using a for-in or for loop under the hood.

+1
Jul 07 '11 at 13:20
source share

Taken from http://api.jquery.com/jQuery.each/

The $.each() function does not match the .each() , which is used to iterate over the jQuery object exclusively. The $.each() function can be used to iterate over any collection, be it a map (JavaScript object) or an array. In the case of an array, the callback is each time passed by the array index and the corresponding array value. (This value can also be accessed through this keyword, but Javascript will always carry this value as an object, even if it is a simple string or a numeric value.) The method returns its first argument, the object that was iterated.

0
Jul 07 2018-11-11T00:
source share

In the first case, you can iterate over jQuery objects, as well as other elements of the array, as indicated here:

jQuery.each ()

In the second case, you can only repeat jQuery objects, as indicated here:

. each ()

0
Jul 07 '11 at 13:21
source share

From what I understand $.each(); goes through an object or array and gives you an iterator and the value of each element.

$().each(); iterates over a list of jQuery objects and gives you an iterator and jQuery object.

0
Jul 07 '11 at 13:21
source share



All Articles