README
polydom
Tiny polyfill for manipulating HTML classes in IE9+ and modern browsers.
Example
var el = document.querySelectAll('.switches');
Polydom(el).each(function() {
if (Polydom(this).hasClass('bar')) {
Polydom(document.getElementById('foo')).toggleClass('baz');
}
});
Install
May be manually downloaded and linked to like any other JavaScript file, or installed via NPM.
npm install polydom
API
addClass
- Argument: className
string
- Returns:
Polydom
Adds a class to DOM elements.
Polydom(el).addClass('blue');
each
- Argument: callback
function
- Returns:
Polydom
Iterate over an array or NodeList. Safe to use on a single Element
. Within the callback, this
refers to the current element.
Polydom(el).each(function(i) {
this.textContent = 'Element #' + i;
});
hasClass
- Argument: className
string
- Returns:
boolean
- Throws:
TypeError
Returns true/false depending on if the element has a class. Throws a TypeError
if used on an array or NodeList. Use with each
to run checks iteratively.
var el = document.getElementById('foo');
if (Polydom(el).hasClass('bar)) {
el.textContent = 'Matching!';
}
removeClass
- Argument: className
string
- Returns:
Polydom
Removes a class from DOM elements.
Polydom(el).removeClass('blue');
toggleClass
- Argument: className
string
- Returns:
Polydom
Adds/removes a class from DOM elements based on whether it's already there.
Polydom(el).toggleClass('blue');
Credit
Derived from You Might Not Need jQuery. Thank you!