Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I check in JavaScript if a DOM element contains a class?

Tags:

How can I check in JavaScript if a DOM element contains a class?

I tried the following code, but for some reason it doesn't work...

if (document.getElementById('element').class == "class_one") {     //code... } 
like image 263
Florian Müller Avatar asked Nov 23 '10 15:11

Florian Müller


People also ask

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.

How do you know if an element contains text?

To check if an element contains specific text: Use the textContent property on the element to get the text content of the element and its descendants. Use the includes() method to check if the specific text is contained in the element. If it is, the includes() method returns true , otherwise false is returned.

What is DOMTokenList?

A DOMTokenList is a parsed representation of a space-separated string of tokens, such as the className property of a Element. A DOMTokenList is, as its name implies, a list—it is an array-like object with a length property and you can index it to retrieve the individual tokens.

How do you check if a particular DOM element has a class?

How do you check if a particular DOM element you have the reference of, has a class? Use the contains method provided by the classList object, which is: Technically, classList is an object that satisfies the DOMTokenList interface, which means it implements its methods and properties. You can see its details on the DOMTokenList MDN page.

How to check if an element contains a class in JavaScript?

Check if an Element Contains a Class in JavaScript Check If an Element contains a Class To check if an element contains a class, you use the contains () method of the classList property of the element: element.classList.contains (className);

What is getElementsByClassName () in JavaScript?

The Function getElementsByClassName () is used to find the element in the DOM using its class name. An example to the class name value is ClassExample in element <a class="ClassExample"></a>. It will return one or more elements if any element is found or null if the element does not exist.

How to find the element using its ID in JavaScript?

Similar to that we use the getElementById () function to find the element using its Id, we also have many other functions that perform the same operation by with deffrernt criteria like getElementsByClassName (), getElementsByName () and getElementsByTagName ().


2 Answers

To get the whole value of the class atribute, use .className

From MDC:

className gets and sets the value of the class attribute of the specified element.

Since 2013, you get an extra helping hand.

Many years ago, when this question was first answered, .className was the only real solution in pure JavaScript. Since 2013, all browsers support .classList interface.

JavaScript:

if(document.getElementById('element').classList.contains("class_one")) {     //code... } 

You can also do fun things with classList, like .toggle(), .add() and .remove().

MDN documentation.

Backwards compatible code:

if(document.getElementById('element').className.split(" ").indexOf("class_one") >= 0) {     //code... } 
like image 102
Greg Avatar answered Oct 28 '22 02:10

Greg


The property you need is className, not class. Also, an element can have many classes, so if you want to test if it has a particular class you need to do something like the following:

function hasClass(el, clss) {     return el.className && new RegExp("(^|\\s)" +            clss + "(\\s|$)").test(el.className); }  var element = document.getElementById('element'); if ( hasClass(element, "class_one") ) {     // Do stuff here } 

UPDATE

Modern browsers (pretty much everything major except IE <= 9) support a classList property, as mentioned in @Dropped.on.Caprica's answer. It therefore makes sense to use this where available. Here's some example code that detects whether classList is supported by the browser and falls back to the regex-based code otherwise:

var hasClass = (typeof document.documentElement.classList == "undefined") ?     function(el, clss) {         return el.className && new RegExp("(^|\\s)" +                clss + "(\\s|$)").test(el.className);     } :     function(el, clss) {         return el.classList.contains(clss);     }; 
like image 23
Tim Down Avatar answered Oct 28 '22 01:10

Tim Down