This post will discuss how to get the CSS class of an element using JavaScript and jQuery.

1. Using jQuery

You can get the value of class attribute using jQuery’s .attr() method.

JS


CSS


HTML



Edit in JSFiddle

 
Alternatively, you can use jQuery’s .prop() method.

JS


CSS


HTML



Edit in JSFiddle

2. Using JavaScript

In plain JavaScript, you can use the className property to get the value of the class attribute of the specified element.

JS


CSS


HTML



Edit in JSFiddle

 
In modern browsers, you can get value of className like:

JS


CSS


HTML



Edit in JSFiddle

That’s all about getting the CSS class of an element using JavaScript and jQuery.