Sunday, May 19, 2024
 Popular · Latest · Hot · Upcoming
17
rated 0 times [  22] [ 5]  / answers: 1 / hits: 73482  / 13 Years ago, tue, february 14, 2012, 12:00:00

I have an element with multiple classes and I'd like to get its css classes in an array. How would I do this? Something like this:



var classList = $(this).allTheClasses();

More From » jquery

 Answers
23

No need to use jQuery for it:



var classList = this.className.split(' ')


If you for some reason want to do it from a jQuery object, those two solutions work, too:



var classList = $(this)[0].className.split(' ')
var classList = $(this).prop('className').split(' ')


Of course you could switch to overkill development mode and write a jQuery plugin for it:



$.fn.allTheClasses = function() {
return this[0].className.split(' ');
}


Then $(this).allTheClasses() would give you an array containing the class names.


[#87469] Monday, February 13, 2012, 13 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
leslijessalyng

Total Points: 650
Total Questions: 85
Total Answers: 109

Location: Croatia
Member since Mon, Sep 6, 2021
3 Years ago
leslijessalyng questions
Fri, Feb 21, 20, 00:00, 4 Years ago
Tue, Jul 30, 19, 00:00, 5 Years ago
Fri, Jul 5, 19, 00:00, 5 Years ago
Wed, Mar 13, 19, 00:00, 5 Years ago
;