className in javascript

className property in javascript

className property is used to return the class name of an element.

Using this property, we can get all the class name of particular element.

Unlike classList, className property can be used only to retrieve the value of an class.


var elem = element.className;

Example - Get value of a class

Try Yourself

Example Explanation

  • In the above example we have created a button (Get class name).
  • In the button we have set five attributes (type, id, value, class, onclick).
  • When the button is clicked, click_func function is called.
  • In the Javascript function, first we are getting the element (document.getElementById("idBtn")) and storing it in elem variable.
  • In the next line we are simply displaying the class name (alert (elem.className)).