classList.add() code example

Example 1: javascript check if element has class

if(document.getElementById("myElmentID").classList.contains("hidden")){
// I have the 'hidden' class
}

Example 2: add multiple class list at once in js

elem.classList.add("first");
elem.classList.add("second");
elem.classList.add("third");

is equal to :

elem.classList.add("first","second","third");

Example 3: javascript loop over classlist

var myElements = document.getElementsByClassName("some_class_name");
for(var i = 0; i < myElements.length; i++){
	console.log(myElements[i]);
}

Example 4: classlist

// use the classList API to remove and add classes
div.classList.remove("foo");
div.classList.add("anotherclass");

Example 5: class list contains class java

List<Card> hand = ...;
for(Card card : hand){
  if (card instanceof AceOfDiamonds) return true;
}

Tags:

Java Example