In vanilla JS, if you want to manipulate a collection of elements, you have to get all the elements and then iterate over the collection:
const paragraphs = [...document.querySelectorAll('p')];
paragraphs.forEach(function(paragraph) {
paragraph.classList.add('text-large');
});
So, you might reasonably assume that you need to do something similar in jQuery, using the .each()
method:
const $paragraphs = $('p');
$paragraphs.each(function(index, paragraph) {
$(paragraph).addClass('text-large');
});
But this is actually superfluous!
Instead, you should rely on implicit iteration:
const $paragraphs = $('p');
$paragraphs.addClass('text-large');
This is something jQuery does internally. Most methods that return a jQuery collection also iterate over the collection, meaning you don't have to.
For more information, check out Iterating over jQuery and non-jQuery Objects in the jQuery Learning Center.