How to check if an HTML element has a class in JavaScript

1 Answer

0 votes
<div class="divclass" id="divid">  
  <p>text</>
</div>
const element = document.getElementById('divid');

console.log(element.classList.contains('divclass'));
console.log(element.classList.contains('user')); 


  
    
    
/*
run:
    
true
false
    
*/

 



answered Jun 12, 2021 by avibootz
...