52

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();
2
  • possible duplicate of Get Class List for Element with jQuery Commented Feb 14, 2012 at 15:26
  • duplicates are the results of different search queries. Some people will google "Get Class List for Element" and others will google "getting the css classes of an element". I did the latter, and others will too. Commented Feb 14, 2012 at 15:34

6 Answers 6

88

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.

Sign up to request clarification or add additional context in comments.

4 Comments

ok, the reason I think I need to do this in jquery is that I'm passing a jquery object as a function's parameter and using the code like this: var classList = TheObject.prop('className').split(' '); It works; is this the best way to do it?
Yes, that's the best way. The plugin would be nicer though if you use it at multiple places in your code.
ok, thanks for the code. I'll come back to the plug-in idea if I need to use this in several places.
classList is an object, indexed as an array, className is a comma-separated string.
28

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.

2 Comments

Actually classList is not an array but a Dom token list so keep in mind you can't use array methods with it straight away.
Array.from(myElement.classList)
9

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 !

Comments

2

Check this out:

var classes = $('selector').prop('classList');

1 Comment

classList is an object, indexed as an array, className is a comma-separated string.
1

function showClasses() {
  const div = document.querySelector('div');
  const classes = div.className.split(' ');

  const p = document.querySelector('p');
  p.innerHTML = classes;
}
<div class="foo bar">This div has foo, bar classes</div>
<p class='output'>Above div classes appear here</p>
<button onClick="showClasses();">Show div classes</button>

HTML

<div class="foo bar">This div has foo, bar classes</div>

Vanilla JavaScript. It will return an array of classes.

const div = document.querySelector('div');
const classes = div.className.split(" "); // ['foo', 'bar']

Comments

1

element.classList.value

console.log("class")
console.log(document.getElementById('c2').classList.value)
<div id="c2" class="class1 class2"> i am two class</div>

getAttribute

    console.log("class")
    console.log(document.getElementById('c2').getAttribute('class'))
    <div id="c2" class="class1 class2"> i am two class</div>

className

   console.log("class")
        console.log(document.getElementById('c2').className)
        <div id="c2" class="class1 class2"> i am two class</div>

to make an array choose any one of above method

string.split(' ');

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.