Is there a way in jQuery to loop through or assign to an array all of the classes that are assigned to an element?
ex.
<div class="Lorem ipsum dolor_spec sit amet">Hello World!</div>
I will be looking for a "special" class as in "dolor_spec" above. I know that I could use hasClass() but the actual class name may not necessarily be known at the time.
I had a similar issue, for an element of type image. I needed to check whether the element was of a certain class. First I tried with:
but I got a nice "this function is not available for this element".
Then I inspected my element on the DOM explorer and I saw a very nice attribute that I could use: className. It contained the names of all the classes of my element separated by blank spaces.
Once you get this, just split the string as usual.
In my case this worked:
I am assuming this would work with other kinds of elements (besides img).
Hope it helps.
I know this is an old question but still.
If you want to manipulate element
If you want to check if element has class
if multiple classes
You should try this one:
It returns an array of all current classes of the element.
Might this can help you too. I have used this function to get classes of childern element..
In your case you want doler_ipsum class u can do like this now
calsses[2];
.A bit late, but using the extend() function lets you call "hasClass()" on any element, e.g.:
var hasClass = $('#divId').hasClass('someClass');