# Yahoo! UI Library

### dom 3.0.0

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

    var addClass, hasClass, removeClass;

    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 && hasClass(node, className)) {
                node.className = Y.Lang.trim(node.className.replace(Y.DOM._getRegExp('(?:^|\\s+)' +
                                className + '(?:\\s+|$)'), ' '));

                if ( hasClass(node, className) ) { // in case of multiple adjacent
                    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');
            addClass(node, newC);
            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 (hasClass(node, className)) {
                removeClass(node, className);
            } else {
                addClass(node, className);
            }
        }
    });

    hasClass = Y.DOM.hasClass;
    removeClass = Y.DOM.removeClass;
    addClass = Y.DOM.addClass;

Copyright © 2009 Yahoo! Inc. All rights reserved.