Return to Snippet

Revision: 34088
at October 17, 2010 04:24 by axolx


Initial Code
Element.prototype.hasClassName = function(name) {
    return new RegExp("(?:^|\\s+)" + name + "(?:\\s+|$)").test(this.className);
};

Element.prototype.addClassName = function(name) {
    if (!this.hasClassName(name)) {
        this.className = this.className ? [this.className, name].join(' ') : name;
    }
};

Element.prototype.removeClassName = function(name) {
    if (this.hasClassName(name)) {
        var c = this.className;
        this.className = c.replace(new RegExp("(?:^|\\s+)" + name + "(?:\\s+|$)", "g"), "");
    }
};

Initial URL
http://www.html5rocks.com/tutorials/dnd/basics/

Initial Description
These are handy when not using a library

Initial Title
Javascript add/remove/toggle class API

Initial Tags

                                

Initial Language
JavaScript