Check if it's the class

4

How do I check with JavaScript if it's really that class in CSS? Example:

if(é a class) {

}

or another example, if not the class

if(!.minhaClasse) {

}

Is there any way to do this with pure JavaScript?

    
asked by anonymous 19.07.2017 / 20:59

2 answers

4

You can do this:

function contemClasse(elemento, classe) {
    for (int i = 0; i < elemento.classList.length; i++) {
        if (elemento.classList[i] == classe) {
            return true;
        }
    }
    return false;
}

And to use:

contemClasse(foo, "nomeDaClasse");
    
19.07.2017 / 21:17
6

With pure javascript you can do this:

<div id="teste" class="teste2"> Teste </div>
<script>
    var div = document.getElementById('teste');
    console.log(div.classList.contains('teste2'));
</script>

For more information, see link

    
19.07.2017 / 21:05