Sometimes we need to check the element has the class name ‘X’ ( Any specific name ). To check if the element contains a specific class name, we can use the contains method of the classList object.
It returns a Boolean value. If the element contains the class name it returns true otherwise it returns false.
Implementation: Now let’s implement the given method. Here we will create a simple HTML page and add an h1 element having the class name main and id name main. Our task is to find the h1 contains the class name main.
- Now check if the element has the class name main and also checking if the class name myClass present or not.