How can I check if body has specific class? This is my case:
<body class="foo foo1 foo3"></body>
Use the classList. contains() method to check if the body element has a specific class, e.g. document. body. classList.
jQuery hasClass() Method The hasClass() method checks if any of the selected elements have a specified class name. If ANY of the selected elements has the specified class name, this method will return "true".
JavaScript classList is a DOM property of JavaScript that allows for styling the CSS (Cascading Style Sheet) classes of an element. JavaScript classList is a read-only property that returns the names of the CSS classes.
Check if specific Class exists on the Page using JS #Use the document. getElementsByClassName() method to select the elements with the class name. Check if the length property of the collection is greater than 0 . If it is, then the class exists on the page.
There's now a super-easy way to do this:
document.body.classList.contains('my-class-name')
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With