Monday, May 20, 2024
 Popular · Latest · Hot · Upcoming
110
rated 0 times [  115] [ 5]  / answers: 1 / hits: 21593  / 14 Years ago, mon, february 7, 2011, 12:00:00

I have a collection of divs...



<div class='happy person'><img src='#' /></div>
<div class='fat person'><img src='#' /></div>
<div class='joyous person'><img src='#' /></div>
<div class='grotesque person'><img src='#' /></div>
<div class='sad person'><img src='#' /></div>


that I have selected using...



var people = $('.person')


The results are stored in a class variable.
jQuery stores the results of this selection as an array of HTMLDivElements - which they are.



Later on, I want to be able to look at this array and make some decisions with respect to the class of each element. I have read up on a possible solution; but this fails since I am not dealing directly with a jQuery object.



How do I get the class names of these divs in the array?


More From » jquery

 Answers
32

This should work:



var people = $('.person');
$.each(people, function(index, el) {
var _this = $(el);
if (_this.hasClass('happy')) {
alert('Happy!');
} else if (_this.hasClass('fat')) {
alert('Fat!');
}
});

[#93868] Friday, February 4, 2011, 14 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
emerald

Total Points: 547
Total Questions: 96
Total Answers: 122

Location: Oman
Member since Fri, Dec 23, 2022
1 Year ago
;