element.className

  • Revision slug: Web/API/element.className
  • Revision title: element.className
  • Revision id: 406315
  • Created:
  • Creator: Sheppy
  • Is current revision? No
  • Comment Moved From DOM/element.className to Web/API/element.className

Revision Content

{{DomRef}}

Summary

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

Syntax

cName = elementNodeReference.className;
elementNodeReference.className = cName;
  • cName is a string variable representing the class or space-separated classes of the current element.

Example

var elm = document.getElementById("div1");

if (elm.className == "fixed") {
  // skip a particular class of element
  goNextElement();
}

Notes

The name className is used for this property instead of class because of conflicts with the "class" keyword in many languages which are used to manipulate the DOM.

Specification

See also

  • {{domxref("element.classList")}}

Revision Source

<div>
  {{DomRef}}</div>
<h2 id="Summary" name="Summary">Summary</h2>
<p><strong>className</strong> gets and sets the value of the <code>class</code> attribute of the specified element.</p>
<h2 id="Syntax" name="Syntax">Syntax</h2>
<pre class="syntaxbox">
<var>cName</var> = <var>elementNodeReference</var>.className;
<var>elementNodeReference</var>.className = <var>cName</var>;</pre>
<ul>
  <li><var>cName</var> is a string variable representing the class or space-separated classes of the current element.</li>
</ul>
<h2 id="Example" name="Example">Example</h2>
<pre class="brush: js">
var elm = document.getElementById("div1");

if (elm.className == "fixed") {
  // skip a particular class of element
  goNextElement();
}</pre>

<h2 id="Notes" name="Notes">Notes</h2>
<p>The name <code>className</code> is used for this property instead of <code>class</code> because of conflicts with the "class" keyword in many languages which are used to manipulate the DOM.</p>
<h2 id="Specification" name="Specification">Specification</h2>
<ul>
  <li><a href="http://www.w3.org/TR/DOM-Level-2-HTML/html.html#ID-95362176">DOM Level 2 HTML: className</a></li>
</ul>
<h2 id="See_also">See also</h2>
<ul>
  <li>{{domxref("element.classList")}}</li>
</ul>
Revert to this revision