Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get html element's class tags

I'm using a custom modernizer config which has selected the features I employ in my page (and only those features).

So, I'd like to simply grab the className of the <html> of the page so I can check to see how many no- prefixed classes are present (maybe checking classlist.match(/no-/g).length) and determine if my javascript should just give up.

It's not clear whether I should use

document.getElementsByTagName('html').className

or

$('html').attr('class')

or

document.documentElement.className
like image 500
Steven Lu Avatar asked Jul 14 '12 20:07

Steven Lu


People also ask

How do you get a class tag in HTML?

To get the class names of a specific HTML Element as String, using JavaScript, get reference to this HTML element, and read the className property of this HTML Element. className property returns the classes in class attribute separated by space, in a String.

What is HTML class element?

The HTML class attribute specifies one or more class names for an element. Classes are used by CSS and JavaScript to select and access specific elements. The class attribute can be used on any HTML element. The class name is case sensitive. Different HTML elements can point to the same class name.

Can I use get element by class?

The JavaScript getElementsByClassName is used to get all the elements that belong to a particular class. When the JavaScript get element by class name method is called on the document object, it searches the complete document, including the root nodes, and returns an array containing all the elements.

Can HTML element have class?

HTML elements can have more than one class name, where each class name must be separated by a space. Example: html.


1 Answers

I will go for:

document.documentElement.className;

Because doesn't involve any function's call, neither an additional layer like jquery. Ideally this one is the cleanest and the fastest.

like image 144
ZER0 Avatar answered Nov 15 '22 15:11

ZER0