Skip to content

Instantly share code, notes, and snippets.

@jonathantneal
Created June 19, 2012 20:05
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jonathantneal/2956234 to your computer and use it in GitHub Desktop.
Save jonathantneal/2956234 to your computer and use it in GitHub Desktop.
classList polyfill // returns a token list of the class names attached to the element.
// classList | @jon_neal | MIT/GPL2
this.Element && !document.lastChild.classList && (function () {
Object.defineProperty(Element.prototype, "classList", {
configurable: true,
get: function () {
var
join = [].join,
push = [].push,
splice = [].splice,
node = this,
list = new function DOMTokenList() {
var list = this, index;
list.contains = function (value) {
var length = list.length;
index = -1;
splice.apply(list, [0, length].concat((node.getAttribute("class") || "").split(/\s+/)));
while (++index < length) if (list[index] == value) break;
return index < length;
};
list.add = function (value) {
if (!list.contains(value)) push.call(list, String(value));
node.setAttribute("class", join.call(list, " "));
};
list.remove = function (value) {
if (list.contains(value)) splice.call(list, index, 1);
node.setAttribute("class", join.call(list, " "));
};
list.toggle = function (value) {
list.contains(value) ? splice.apply(list, [index, 1]) : push.call(list, String(value));
node.setAttribute("class", join.call(list, " "));
};
list.contains("");
};
return Object.defineProperties(node, {
className: {
configurable: true,
get: function () {
return join.call(list, " ");
},
set: function (value) {
this.setAttribute("class", value);
list.contains("");
}
},
classList: {
configurable: true,
get: function () {
return list;
}
}
}).classList;
}
});
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment