jQuery to loop through elements with the same class jQuery to loop through elements with the same class javascript javascript

jQuery to loop through elements with the same class


Use each: 'i' is the postion in the array, obj is the DOM object that you are iterating (can be accessed through the jQuery wrapper $(this) as well).

$('.testimonial').each(function(i, obj) {    //test});

Check the api reference for more information.


try this...

$('.testimonial').each(function(){    //if statement here     // use $(this) to reference the current div in the loop    //you can try something like...    if(condition){    } });


It's pretty simple to do this without jQuery these days.

Without jQuery:

Just select the elements and use the .forEach() method to iterate over them:

const elements = document.querySelectorAll('.testimonial');Array.from(elements).forEach((element, index) => {  // conditional logic here.. access element});

In older browsers:

var testimonials = document.querySelectorAll('.testimonial');Array.prototype.forEach.call(testimonials, function(element, index) {  // conditional logic here.. access element});