How to get all CSS classes of an element?

Note that you can also use myElement.classList as a simple array-like object:

const classList = myElement.classList;

This is supported by all major browsers for a while now, apart IE 9 and below.


This should do the work for you:

var classes = $('div').attr('class').split(" ");

This would be the jQuery solution for other solutions there are other answers !


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.