jQuery each()
The jQuery each()
function is a convenient way to iterate over a set of elements and perform a certain action on each element individually.
Syntax
The syntax for each()
function is as follows:
$.each( collection, callback )
Where collection
is the set of elements to iterate over (e.g. an array or object) and callback
is the function to execute for each element. The callback
function takes two parameters: index
and value
, which correspond to the current element's index and value, respectively.
Use
The each()
function is useful for performing a specific action on each element in a set. It can be used in a variety of scenarios, such as iterating over a list of items, applying a function to each element in a collection, or even updating the state of multiple elements on a page.
Example
Here's an example of using the each()
function to iterate over an array of names and display them in an unordered list: