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.
Syntax:
element.classList
- 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');
div1.classList.remove('cls_2');
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');
div1.classList.toggle('visible');

Daily Test with Code Example

HTML
CSS
JavaScript
PHP-MySQL
Which tag defines the clickable areas inside the image map?
<map> <img> <area>
<img src="image.jpg" usemap="#map1">
<map name="map1">
  <area shape="rect" coords="9, 120, 56, 149" href="#">
  <area shape="rect" coords="100, 200, 156, 249" href="#">
</map>
Which CSS property defines what is done if the content in a box is too big for its defined space?
display overflow position
#id {
  overflow: auto;
}
Click on the event which is triggered when the mouse is positioned over an object.
onclick onmouseover onmouseout
document.getElementById("id").onmouseover = function(){
  document.write("Have Good Life");
}
Indicate the PHP variable that contains data added in URL address after the "?" character.
$_SESSION $_GET $_POST
if(isset($_GET["id"])) {
  echo $_GET["id"];
}
classList - Working with CSS classes

Last accessed pages

  1. Get Attribute (ID, Class, Name, Title, Src) with jQuery (51302)
  2. Bubble Tanks Tower Defense 2 (864)
  3. Sport Games (12462)
  4. Rollercoaster Creator (502)
  5. Bubble Tanks Tower Defence (604)

Popular pages this month

  1. Flash Games - Free online Games (1337)
  2. Qwop (1150)
  3. Tunnel Rush (1058)
  4. Courses Web: PHP-MySQL JavaScript Ajax HTML CSS Flash-AS3 (967)
  5. Drag Racer V3 (750)