How to call jQuery .html() on all matched elements?

Sometimes, we want to call jQuery .html() on all matched elements.

In this article, we’ll look at how to call jQuery .html() on all matched elements.

How to call jQuery .html() on all matched elements?

To call jQuery .html() on all matched elements, we can use the spread operator and the for-of loop.

For instance, we write:

<p>
  foo
</p>
<p>
  bar
</p>
<p>
  baz
</p>

to add some elements.

Then we write:

for (const el of [...$('p')]) {
  console.log($(el).html());
};

We select the p elements with $('p').

Then we spread the elements into an array.

Next, we loop through them with a for-of loop.

Then we select the el element with $(el) and call html to return the HTML of el.

Conclusion

To call jQuery .html() on all matched elements, we can use the spread operator and the for-of loop.