Skip to content

toggleClass appends existing classes to className #106

Description

@tboothman

If the browser does not support classList toggleClass does not have the same behaviour.
If the toggled class already exists on an element toggleClass will add that same class to the className string, which means the class can never be removed.

This is caused by the internal addClass function not checking for the new class existing in the className string before appending.

I suppose the fix would be something like:

addClass = function (el, c) {
    if (!hasClass(el,c) {
        el.className = trim(el.className + ' ' + c);
    }
}

That would unfortunately make the addClass API call hasClass twice.
However, given that add/removeClass API functions now have the same functionality as their classList equivalents the hasClass checks are now unnecessary and can be removed.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions