To perform a certain task first, we need to access that specific element by its class or ID, so before accessing that element, we check if the class exists in that specific element. The classList object contains the built-in method classList.contains() in JavScript. This method determines whether the given element belongs to the specified class. The whole process will happen because first we have to access the element through getElementById(), getElementsByClassName() or any other method. After accessing it we have to check the class using classList.contains() method.
The syntax used in this question is -
classList.contains(className);
classList- This is an object in JavaScript that receives an array of classes contained in a specific element.
contains- This is a method of classList object that checks whether the specified class is present in the given element.
className- This is the specified name that we have to search for in the given element.
Step 1- Create some HTML elements inside the body tag. Assign some class to each element.
Step 2- Specify the onclick() event method in the HTML button.
Step 3- Create a JavaScript arrow function. Access any HTML and store it in a variable.
Step 4- Use the contains() method of the classList object. Pass variables as arguments to the contains() method.
Step 5- If it returns true then the specific class exists in the HTML element, else if it returns false then the specific class does not exist in the element.
We used the "
" tag in the body tag, which contains the class name: class = "my-para first lorems", so these are the class names. Our task is to check the element to see if it contains the specified element. To do this, we use the contains() method, which is a method of the classList object. Therefore, the class we want to check is passed as a parameter to the "contains()" method, which checks the certainty of the class.
Check for specified class in a given element I am para with certain classes, check now.
The output of the above example, because the output of "Elements contains specified class" is true.
The following image shows "Element does not contain the specified class", which means that when classList.contains() is checked, it must have returned false. Therefore the error condition has terminated.
Check for pecified class in a given element I am para with certain classes, check now.
The return type of classList is DOMTokenList, which is an array type. It contains the list of classes present in that particular element. The DOMTokenList can be viewed by iterating it using any for loop or map.
var ptag = document.getElementById("para").classList; ptag.forEach(element => { console.log(element); });
The "contains()" method returns a Boolean result, which is true or false. The classList object contains an array of classes. So when the contains() method checks the specified class, it checks the DOMTokenList and makes a decision on its behalf and returns true or false.
The above is the detailed content of How to check if a given element has a specified class in JavaScript?. For more information, please follow other related articles on the PHP Chinese website!