Created
October 30, 2012 14:32
-
-
Save rlemon/3980520 to your computer and use it in GitHub Desktop.
class manipulation.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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