Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to check if a div has a class out of multiple classes in jquery

I have a dynamic div which can generate over 1000 different classes... its a wordpress theme.

Now i am wanting to check if a div has one of 52 classes.

.bg1, .bg2, .bg3 etc etc...

I know that you can use hasClass();

But how do I check for each one and then get the value. For instance here is the div as it stands

<div id="wordpresspost" class="bodyclass post-id-193 wordpresswrap bg1"></div>

Please bare in mind I dont really know jquery :D

I was thinking it would be something like this but logically this does not make sense to me :(

var divclass = $("#wordpresspost").hasClass('bg1, bg2, bg3, bg4, bg5, bg6');

if(divclass == true){
    var divsclass = $(this);
}

I need to know the class because I want to change it, for instance i would like to .removeClass and then addClass a new class without changing the others as they are dynamic

Thanks in advance for the advice :)

like image 577
Robert Avatar asked Apr 25 '13 19:04

Robert


1 Answers

Use .is():

var divclass = $("#wordpresspost").is('.bg1, .bg2, .bg3, .bg4, .bg5, .bg6');
like image 179
Joe Avatar answered Oct 18 '22 12:10

Joe