I'm trying to use JS to check if an html tag contains a specific class
What I have so far (doesn't work)
if (html.classList.contains('test')) {
}
<html class="test">
I'm trying to use JS to check if an html tag contains a specific class
What I have so far (doesn't work)
if (html.classList.contains('test')) {
}
<html class="test">
You can determine the class within the classList of the element.
let el = document.querySelector('html');
var classList = el.classList;
console.log(classList.contains('test')); // gives true
<html class="test">
You can also grab the className (which is a string of all applied classes and use .indexOf();
let el = document.querySelector('html');
var className = el.className;
console.log(className); // gives 'test test2'
console.log(className.indexOf('test') > -1); // gives true
<html class="test test2">
If you have access to jQuery, the .hasClass() method is what you want
let test = $('html').hasClass('test');
console.log(test); // gives true
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<html class="test">