classList fallback (IE & older browsers) I'm trying to make some JS code more browser compatible. So I've written an alternative to JS's classList providing its .add(), .remove() and .toggle() functionality because it is not or just partially supported in IE.

(I know there are exiting polyfills, but I like to have a good overview regarding my code.)

My test were successful but before I use it extensively I wanted to check with someone if they see any issues.

    function classes(type, element, targetClass){
            var classArray = element.className.split(/\s/);
            var index = classIndex(classArray, targetClass);
            // if target class is in class list
            if(index !== null){
                // multiple classes
                if(classArray.length &gt; 1){
                    if(type == "remove" || type == "toggle") {
                        classArray.splice(index, 1);
                        element.className = classArray.join(" ");
                    }
                }
                // single class
                else {
                    if(type == "remove" || type == "toggle") {
                        element.className = "";
                    }
                }
            }
            // if target class is not in class list
            else {
                if(type == "add" || type == "toggle") {
                    element.className += " " + targetClass;
                }
            }
            // check if target class is in class list (returns index or null)
            function classIndex(array, targetClass){
                for(i=0; i&lt;array.length; i++){
                    if(array[i]==targetClass){
                        return i;
                    }
                    if(i==(array.length-1)){
                        return null;
                    }
                }
            }
        }


Usage:

classes("toggle", myElement, "classToToggle")
I'm not a big fan of neither the function name classes nor the parameter name type. Something like modifyClassOfElement and action would be better fitting. 

You should define constants for the type/action values.

It's convention in JavaScript to have a space between keywords and a following open bracket: if  (

In classIndex the second if is unnecessary. Just put return null; after the loop. Or, even better, you can replace the whole function with an .indexOf() call. 

Move if (type == "remove" || type == "toggle") { up one level so you don't need it twice. Alternatively I'd just drop the special case for a single class, as it's an tiny optimization that is hardly worth it.

Remove one level of brackets on the final else: 

else if (type == "add" || type == "toggle") {
  element.className += " " + targetClass;
}


I'd consider throwing an exception, if the type/action is unknown to aid debugging.

It would be nice for the function to use classList if it's available. Personnally I'd implement this as a function that returns an existing classList or an object that implements the same interface (DOMTokenList)