classList is a JavaScript property that can be used to work with CSS classes on HTML elements, useful to add, remove, replace and toggle CSS classes on an element. It contains an object list of the class attribute.
- If the class attribute was not set or is empty, the element.classList.length returns 0.

classList Methods

The object contained by the classList property has these methods:

classList Examples

1. Add two css classes to a <div>:
var div1 = document.getElementById('div1');
div1.classList.add('cls_1', 'cls_2');
2. Remove a css class from a <div>:
var div1 = document.getElementById('div1');
3. Check if a <div> contains or not a specified css class:
var div1 = document.getElementById('div1');
if(div1.classList.contains('cls_1')) alert('.cls_1 in #div1');
else alert('.cls_1 not in #div1');
4. Replace class 'foo' with 'bar':
var div1 = document.getElementById('div1');
div1.classList.replace('foo', 'bar');
5. If 'visible' is set remove it, otherwise add it:
var div1 = document.getElementById('div1');

Daily Test with Code Example

Which tag adds a new line into a paragraph?
<b> <br> <p>
First line ...<br>
Other line...
Which CSS property can be used to add space between letters?
text-size word-spacing letter-spacing
#id {
  letter-spacing: 2px;
What JavaScript function can be used to get access to HTML element with a specified ID?
getElementById() getElementsByTagName() createElement()
var elm = document.getElementById("theID");
var content = elm.innerHTML;
Click on the "echo" correct instruction.
echo "" echo ""; echo """;
echo "Address URL:";
classList - Working with CSS classes

Last accessed pages

  1. The Mastery of Love (2848)
  2. PHP PDO - Select query, fetch (19402)
  3. jQuery UI draggable - Drag elements (9673)
  4. Get search data from URL address in JavaScript (2097)
  5. Read Excel file data in PHP - PhpExcelReader (52536)

Popular pages this month

  1. Qwop (4885)
  2. Get Attribute (ID, Class, Name, Title, Src) with jQuery (2982)
  3. Get the value of the selected /checked checkboxes in a form (2320)
  4. Drag Racer V3 (1932)
  5. Flash Games - Free online Games (1569)