Test JQuery classes

3

I know that to test if an element has a class we use hasClass (), but my question is as follows. I have an element with a class and assign a second class in it with classToggle (). Is there a way to test if a class has another class? If you're confused, I'll be answering and editing everything.

    
asked by anonymous 10.07.2014 / 16:07

1 answer

4

I do not know if this fully answers your question, but you can retrieve the classes as follows.

Example:

HTML

<div id="minhaDiv" class="classe1 classe2 classe3"></div>

JS

var classes = document.getElementById("minhaDiv").className.split(' ');
console.log(classes);

or

Jquery

var classes = $("#minhaDiv").attr("class").split(' ');
console.log(classes);

The variable will be populated with an Array containing all the classes that are set in the element.

Result (Console)

  

["class1", "class2", "class3"]

DEMO - Example

And if you support the element.classList browser, just do so that the backup will be the same.

Example 2:

JS

var classes = document.getElementById("minhaDiv").classList;
console.log(classes);

DEMO - Example 2

    
10.07.2014 / 16:21