jQuery to loop through elements with the same class

geoffs3310 picture geoffs3310 · Jan 19, 2011 · Viewed 817.5k times · Source

I have a load of divs with the class testimonial and I want to use jquery to loop through them to check for each div if a specific condition is true. If it is true, it should perform an action.

Does anyone know how I would do this?

Answer

Kees C. Bakker picture Kees C. Bakker · Jan 19, 2011

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.