Questions tagged [each]

An iterator function or language struct which can be used to iterate over arrays or lists.

A generic iterator function, which can be used to seamlessly iterate over both objects and arrays. Arrays and array-like objects with a length property (such as a function's arguments object) are iterated by numeric index, from 0 to length-1. Other objects are iterated via their named properties.

jQuery.each( array, callback )
// or
jQuery.each( object, callback )
3858 questions
15
votes
4 answers

In jQuery is it more efficient to use filter(), or just do so in the each()?

I currently have code that is pulling in data via jQuery and then displaying it using the each method. However, I was running into an issue with sorting, so I looked into using, and added, jQuery's filter method before the sort (which makes…
James Skemp
  • 8,018
  • 9
  • 64
  • 107
15
votes
6 answers

JQuery - How to add a class to every last list item?

Got some code here that isn't working: $("#sidebar ul li:last").each(function(){ $(this).addClass("last"); }); Basically I have 3 lists and want to add a class (last) to each item appearing last in each unordered list.
  • Item…
Keith Donegan
  • 26,213
  • 34
  • 94
  • 129
15
votes
2 answers

jQuery .each() vs. .map() without return

Is there any difference between .each() and .map() when no value is returned? Is there any benefit in using one or the other in this case? myList.map(function(myModel, myIndex){ myModel.itemOne = itemOne; myModel.itemTwo =…
neridaj
  • 2,143
  • 9
  • 31
  • 62
15
votes
2 answers

How to append multiple file inputs to a FormData object using $.each?

I have a multiple (and dynamic) number of inputs of type=file. I want to create a FormData object out of them. I need to manually append them to the object as I need access to their filenames for inserting into a database and therefore need to…
user1063287
  • 10,265
  • 25
  • 122
  • 218
15
votes
10 answers

Get label for input field

I'm doing a validation with Jquery and need to get the $label from each element with their own label. Now the alert() gives med [object object]. The best thing for me here is to get an alert() with all fields lined up that is not filled out. And…
Kim
  • 1,128
  • 6
  • 21
  • 41
15
votes
4 answers

Iterating over JSON object in jquery

I have a json object as [ {"DisplayName":"Answer Number 1","Value":"Answer1","Option":"True"}, {"DisplayName":"Answer Number 1","Value":"Answer1","Option":"False"}, {"DisplayName":"Answer Number…
Gautam
  • 1,728
  • 8
  • 32
  • 67
15
votes
5 answers

Chain ajax and execute it in sequence. Jquery Deferred

I have 3 processes that needs ajax to complete. But it is asynchronous and it fails to do what I wanted to do.. Lets say: function a(param1, param2) { $.post(..., function(result){ if(result){ b(); } else { …
Joey Hipolito
  • 3,108
  • 11
  • 44
  • 83
15
votes
5 answers

jquery for each all elements having similar id

I have many elements accross page - ID1, ID2 ID3 ... I want to manipulate all the elements. Is there any simpler way to do this. $("#ID").each(function(){ ... });
Ashwin
  • 12,081
  • 22
  • 83
  • 117
14
votes
3 answers

jQuery: How to use each starting at an index other than 0

I have a collection of elements that I want to loop over using each, but I am looping over them inside an outer for loop. When I find what I want in the each, I return false to break out. The next time the outer loop runs, I want to start in the…
Carvell Fenton
  • 2,341
  • 6
  • 23
  • 30
14
votes
4 answers

setTimeout inside $.each()

ok, so I've got this code: $(this).find('article.loading').each( function(i) { var el = this; setTimeout(function () { $(el).replaceWith($('#dumpster article:first')); }, speed); }); I want to replace each element with…
GreenDude
  • 567
  • 2
  • 5
  • 14
13
votes
2 answers

jQuery - Create an array of objects using each

I'm a jQuery newbie. I have a simple form with n lines (although I'm not using html form):
City1:
City2:
City3:…
idophir
  • 14,451
  • 5
  • 24
  • 21
13
votes
0 answers

PHP 7.2 deprecated: while = each() loop without $value

As each() loop is deprecated since PHP 7.2, how to update the below while(() = each()) loop without $value? Without the $value I can't get foreach loop to work. In addition while($products_id = $this->contents) results in infinite loop. Thank…
Petro Mäntylä
  • 711
  • 1
  • 7
  • 9
13
votes
2 answers

Is there a difference between Enumerable#each and Enumerable#each_entry in Ruby?

The Enumerable documentation does not state explicitly that each is an alias for each_entry, but the description of each_entry matches exactly what I would expect from each. In the examples of both answers new classes are defined, which implement…
Alexander Popov
  • 23,073
  • 19
  • 91
  • 130
13
votes
4 answers

jQuery each always sort it?

I have this object in JS: var list = {134 : "A",140 : "B",131 : "C"} I run it with: jQuery.each(list, function(key, value) { console.log(key + " - " + value); }); The output should be: 134 - A 140 - B 131 - C But I dont know why, the output…
Klian
  • 1,520
  • 5
  • 21
  • 32
13
votes
4 answers

Jquery .each through Divs inside another Div

I currently have the following html: Apologies for edit, I was not paying attention when i wrote this.
1
2
3
I am using the JQuery .each command to iterate through each div…
James Mclaren
  • 666
  • 4
  • 10
  • 25