I have some elements that when clicked will fire some action and select the “class” attribute.
in jQuery it’s simple by using $(this).attr('class')
but the problem is that when we want to select one class from multiple classes like when we have for example:
<a href="#" class="class1 class2"></a>
and we want to get only the first class (class1) because when we use this: $(this).attr('class') we will get “class1 class2” as a result.
any idea?
To get the first class:
However, the idea of classes is that they are unordered, hence there’s no jQuery method to do this directly. You may wish to include a
data-attribute instead:With jQuery 1.4.3+ you can directly access the
data-targetattribute with: