Collection of useful JavaScript functions and patterns




// Does the node have a class
function hasClass(node, className) {
    if (node.className) {
        return node.className.match(
            new RegExp('(\\s|^)' + className + '(\\s|$)'));
    } else {
        return false;
    }
};


// Add a class to an node
function addClass(node, className) {
    if (hasClass(node, className)) node.className += " " + className;
};


// Removes a class from an node
function removeClass(node, className) {
    if (hasClass(node, className)) {
        var reg = new RegExp('(\\s|^)' + className + '(\\s|$)');
        node.className = node.className.replace(reg, ' ');
    }
};



from here http://codebits.glennjones.net/cheatsheet/javascript.htm

Комментарии