Skip to content

Instantly share code, notes, and snippets.

@rlemon
Created October 30, 2012 14:32
Show Gist options
  • Save rlemon/3980520 to your computer and use it in GitHub Desktop.
Save rlemon/3980520 to your computer and use it in GitHub Desktop.
class manipulation.
Element.prototype.addClass = function(c) {
if (!this.hasClass(c)) {
this.className += " " + c;
}
return this;
};
NodeList.prototype.addClass = function(c) {
for (var i = 0, l = this.length; i < l; i++) {
this[i].addClass(c);
}
return this;
};
Element.prototype.removeClass = function(c) {
this.replaceClass(c, "");
return this;
};
NodeList.prototype.removeClass = function(c) {
for (var i = 0, l = this.length; i < l; i++) {
this[i].removeClass(c);
}
return this;
};
Element.prototype.replaceClass = function(oC, nC) {
this.className = this.className.replace(new RegExp("(\\b|^)" + oC + "(\\b|$)"), nC);
return this;
};
NodeList.prototype.replaceClass = function(oC, nC) {
for (var i = 0, l = this.length; i < l; i++) {
this[i].replaceClass(oC, nC);
}
return this;
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment