HTML DOM classNameProperty
Definition and Usage
The className property sets or returns the class attribute of an element.
Syntax
Get the attribute value:
HTMLElementObject.className
Set the attribute value:
HTMLElementObject.className=classname
Browser Support
![]()
![]()
![]()
![]()
![]()
All major browsers support the className property
Example
Example
Return the class attribute of the body element:
<html>
<body id="myid" class="mystyle">
<script>
var x=document.getElementsByTagName('body')[0];
document.write("Body CSS class: " + x.className);
document.write("<br>");
document.write("An alternate way: ");
document.write(document.getElementById('myid').className);
</script>
</body>
</html>
<body id="myid" class="mystyle">
<script>
var x=document.getElementsByTagName('body')[0];
document.write("Body CSS class: " + x.className);
document.write("<br>");
document.write("An alternate way: ");
document.write(document.getElementById('myid').className);
</script>
</body>
</html>
Output result:
Body CSS class: mystyle
An alternate way: mystyle
An alternate way: mystyle
Try it yourself »
Other Extensions
Element Object