Iterating Through the jQuery Object Set Using .each()

$("p").each(function (idx){     $(this).html("This is paragraph " + idx);   });

The .each(function) method is one of the most important jQuery object methods because it allows you to traverse all elements in the jQuery set and perform actions on each of them individually. This is different from just applying the same action to all items in the query.

The .each() method allows you to specify a function to run for each element in the jQuery object set. The function is passed an index number as the first argument. Inside the function, the this variable points to the current DOM element.

To illustrate using .each(), check out the following snippet of code. It ...

Get jQuery and JavaScript Phrasebook now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.