14 May 2023

In jQuery, you can use the $.each() method to loop through a collection of elements or objects. In vanilla JavaScript, you can achieve the same functionality using the forEach() method, which is available on arrays.

Source code viewer
  1. // JQUERY
  2. $('.my-class').each(function(index, element) {
  3. // Your code here
  4. });
  5.  
  6. // VANILLA JAVASCRIPT
  7. document.querySelectorAll('.my-class').forEach(function(element, index) {
  8. // Do something with each element
  9. });
Programming Language: ECMAScript