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?
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?
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");
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