# Yahoo! UI Library

### dom 3.0.0b1

Yahoo! UI Library > dom > dom-class.js (source view)
Search:
Filters Show Private Show Protected Show Deprecated

    Y.mix(Y.DOM, {
        /**
         * Determines whether a DOM element has the given className.
         * @method hasClass
         * @param {HTMLElement} element The DOM element.
         * @param {String} className the class name to search for
         * @return {Boolean} Whether or not the element has the given class.
         */
        hasClass: function(node, className) {
            var re = Y.DOM._getRegExp('(?:^|\\s+)' + className + '(?:\\s+|$)');
            return re.test(node.className);
        },

        /**
         * Adds a class name to a given DOM element.
         * @method addClass
         * @param {HTMLElement} element The DOM element.
         * @param {String} className the class name to add to the class attribute
         */
        addClass: function(node, className) {
            if (!Y.DOM.hasClass(node, className)) { // skip if already present
                node.className = Y.Lang.trim([node.className, className].join(' '));
            }
        },

        /**
         * Removes a class name from a given element.
         * @method removeClass
         * @param {HTMLElement} element The DOM element.
         * @param {String} className the class name to remove from the class attribute
         */
        removeClass: function(node, className) {
            if (className && Y.DOM.hasClass(node, className)) {
                node.className = Y.Lang.trim(node.className.replace(Y.DOM._getRegExp('(?:^|\\s+)' +
                                className + '(?:\\s+|$)'), ' '));

                if ( Y.DOM.hasClass(node, className) ) { // in case of multiple adjacent
                    Y.DOM.removeClass(node, className);
                }
            }
        },

        /**
         * Replace a class with another class for a given element.
         * If no oldClassName is present, the newClassName is simply added.
         * @method replaceClass
         * @param {HTMLElement} element The DOM element.
         * @param {String} oldClassName the class name to be replaced
         * @param {String} newClassName the class name that will be replacing the old class name
         */
        replaceClass: function(node, oldC, newC) {
            //Y.log('replaceClass replacing ' + oldC + ' with ' + newC, 'info', 'Node');
            Y.DOM.addClass(node, newC);
            Y.DOM.removeClass(node, oldC);
        },

        /**
         * If the className exists on the node it is removed, if it doesn't exist it is added.
         * @method toggleClass
         * @param {HTMLElement} element The DOM element.
         * @param {String} className the class name to be toggled
         */
        toggleClass: function(node, className) {
            if (Y.DOM.hasClass(node, className)) {
                Y.DOM.removeClass(node, className);
            } else {
                Y.DOM.addClass(node, className);
            }
        }
    });

Copyright © 2009 Yahoo! Inc. All rights reserved.