HiveBrain v1.2.0
Get Started
← Back to all entries
patternjavascriptCritical

jQuery to loop through elements with the same class

Submitted by: @import:stackoverflow-api··
0
Viewed 0 times
withjqueryclasssametheloopthroughelements

Problem

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?

Solution

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.

Code Snippets

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

Context

Stack Overflow Q#4735342, score: 1379

Revisions (0)

No revisions yet.