Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

jquery

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 being selected and acted upon looks like this:

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

I've gone over the jQuery documentation, but I still don't understand the difference. (Is it me or is that documentation sometimes slightly "nebulous" in clarity of content?)

Added Info:

Apparently my attempt a generic examples is confusing people! Along with the (previously) missing parenthesis in the first example. :(

The first example comes from a line in my code that removes the <tbody> for any rows with a checkbox that is checked:

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

The second example comes from a situation where I look through the #classesTable for any checked checkboxes and remove its matching item in a dropdown.

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

I understand that they do two different things, but not to the point that I'd be able to 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?

like image 264
marky Avatar asked Jul 07 '11 13:07

marky


People also ask

What is the use of jQuery each () function?

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.

What is the main difference between selectors and filters?

jQuery selector selects all elements based on the elements name given by you. jQuery filter( ) adds further detail to the selected elements by specifying the criteria of selection.

What is $() in jQuery?

$() = window. jQuery() $()/jQuery() is a selector function that selects DOM elements. Most of the time you will need to start with $() function. It is advisable to use jQuery after DOM is loaded fully.


1 Answers

Description:

.each is an iterator that is used to iterate over only jQuery objects collection while jQuery.each ($.each) is a general function for iterating over JavaScript objects and arrays.


Examples

1) Using $.each() function

var myArray = [10,20,30];  $.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 

2) Using .each() method

$('#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 

Resources

  • https://api.jquery.com/jquery.each/
  • http://api.jquery.com/each/
  • jQuery to loop through elements with the same class
like image 95
Phil Avatar answered Sep 23 '22 20:09

Phil