Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Check if a class exist within a group of selected elements

I have a group of rows selected and I am now trying to determine whether or not they contain a specific class.

I have tried it with hasClass without success as well as with find:

var group = $('table').find('[data-group="group1"]');

//this doesn't work, it always enters in the condition
if(group.find('.active')){
    alert("Founded?");
    group.addClass('green');    
}

http://jsfiddle.net/kAHyA/1/

I have also tried it with if(group.find('.active').length) but still not getting the correct result:

http://jsfiddle.net/kAHyA/3/

like image 213
Alvaro Avatar asked Dec 07 '25 23:12

Alvaro


1 Answers

You can do this with hasClass method http://api.jquery.com/hasClass/

if(group.hasClass('active')){
...
}

If you tried this :

if(group.hasClass('.active')){
...
}

It certainly won't work, note the difference with the "."

like image 183
mguimard Avatar answered Dec 10 '25 12:12

mguimard



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!