Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How check if body has a specific class with JavaScript?

Tags:

How can I check if body has specific class? This is my case:

<body class="foo foo1 foo3"></body> 
like image 853
Wornout Avatar asked Mar 02 '12 11:03

Wornout


People also ask

How do you know if a class exists in your body?

Use the classList. contains() method to check if the body element has a specific class, e.g. document. body. classList.

How do you check if an element contains a class in jQuery?

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".

What does classList do in JavaScript?

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.

Is class present in JavaScript?

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.


1 Answers

There's now a super-easy way to do this:

document.body.classList.contains('my-class-name') 
like image 189
gradosevic Avatar answered Oct 21 '22 07:10

gradosevic