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 HTML5 tag can be used to embed an external application (SWF, PDF) in web page?
<mark> <embed> <canvas>
<embed src="flash_game.swf" width="450" height="350" />
Which CSS pseudo-element adds a special style to the first line of a text?
:first-letter :before :first-line
#id:first-line {
  font-weight: bold;
  color: blue;
Click on the window object property which gets or sets the URL of current page.
window.location window.self window.status
var url = window.location;
Indicate the PHP function used to get the contents of a file or page and store it into a string.
fopen() file_put_contents() file_get_contents()
$homepage = file_get_contents("");
echo $homepage;
classList - Working with CSS classes

Last accessed pages

  1. Hot Rod Master (506)
  2. Add, Change, and Remove Attributes with jQuery (29846)
  3. Making DIV Contents Scroll Horizontally, with multiple Div`s inside (7539)
  4. Flash Games - Free online Games (21340)
  5. Dirt Drift Racing (1019)

Popular pages this month

  1. Qwop (4958)
  2. Drag Racer V3 (2389)
  3. Flash Games - Free online Games (1184)
  4. Courses Web: PHP-MySQL JavaScript Ajax HTML CSS Flash-AS3 (1038)
  5. Bubbles3 (1016)