Class Attributes Flashcards
Add single class to element.
element.addClass(‘foo’);
element.classList.add(‘foo’);
Add multiple classes
element.addClass(‘a b c’);
element.classList.add(‘a’, ‘b’, ‘c’);
Add class(es) to a set of elements
elements.addClass(‘foo’);
ES6
elements.forEach(x => x.classList.add(‘foo’));
ES5
elements.forEach(function(element) { element.classList.add(‘foo’);
});
Remove a single class
element.removeClass(‘foo’);
element.classList.remove(‘foo’);
Remove multiple classes
element.removeClass(‘a b c’);
element.classList.remove(‘a’, ‘b’, ‘c’);
Remove classes from a set of elements.
elements.removeClass(‘foo’);
ES6
elements.forEach(x => x.classList.remove(‘foo’));
ES5
elements.forEach(function(element) { element.classList.remove(‘foo’);
});
element.toggleClass(‘foo’);
element.classList.toggle(‘foo’);
Conditionally toggle a class
element.toggleClass(‘foo’, codition);
condition result is either true or false
element.classList.toggle(‘foo’, codition);
Toggle class on a set of elements.
elements.toggleClass(‘foo’);
ES6
elements.forEach(x => x.classList.toggle(‘foo’));
ES5
elements.forEach(function(element) { element.classList.toggle(‘foo’);
});
Check if a single element has a class.
var has = element.hasClass(‘foo’);
var has = element.classList.contains(‘foo’);
Check if at least one of the elements in a set of elements has a certain class
var has = elements.hasClass(‘foo’);
ES6
var has = elements.some(x => x.classList.contains(‘foo’));
ES5
var has = elements.some(function(element) { return element.classList.contains(‘foo’);
});